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: 99d89ea8d8a69ab199f488532502092964ed473a $
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: 99d89ea8d8a69ab199f488532502092964ed473a $")
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
63
66 { .out = &attr_packet_type, .name = "Packet-Type", .type = FR_TYPE_UINT32, .dict = &dict_radius },
67 { .out = &attr_packet_authentication_vector, .name = "Packet-Authentication-Vector", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
68 { .out = &attr_chap_challenge, .name = "CHAP-Challenge", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
69 { .out = &attr_chargeable_user_identity, .name = "Chargeable-User-Identity", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
70
71 { .out = &attr_eap_message, .name = "EAP-Message", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
72 { .out = &attr_message_authenticator, .name = "Message-Authenticator", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
73 { .out = &attr_state, .name = "State", .type = FR_TYPE_OCTETS, .dict = &dict_radius },
74 { .out = &attr_vendor_specific, .name = "Vendor-Specific", .type = FR_TYPE_VSA, .dict = &dict_radius },
75 { .out = &attr_nas_filter_rule, .name = "NAS-Filter-Rule", .type = FR_TYPE_STRING, .dict = &dict_radius },
76
78};
79
80/*
81 * Some messages get printed out only in debugging mode.
82 */
83#define FR_DEBUG_STRERROR_PRINTF if (fr_debug_lvl) fr_strerror_printf
84#define FR_DEBUG_STRERROR_PRINTF_PUSH if (fr_debug_lvl) fr_strerror_printf_push
85
94
103
114
116 "", //!< 0
117 "Access-Request",
118 "Access-Accept",
119 "Access-Reject",
120 "Accounting-Request",
121 "Accounting-Response",
122 "Accounting-Status",
123 "Password-Request",
124 "Password-Accept",
125 "Password-Reject",
126 "Accounting-Message", //!< 10
127 "Access-Challenge",
128 "Status-Server",
129 "Status-Client",
130 "14",
131 "15",
132 "16",
133 "17",
134 "18",
135 "19",
136 "20", //!< 20
137 "Resource-Free-Request",
138 "Resource-Free-Response",
139 "Resource-Query-Request",
140 "Resource-Query-Response",
141 "Alternate-Resource-Reclaim-Request",
142 "NAS-Reboot-Request",
143 "NAS-Reboot-Response",
144 "28",
145 "Next-Passcode",
146 "New-Pin", //!< 30
147 "Terminate-Session",
148 "Password-Expired",
149 "Event-Request",
150 "Event-Response",
151 "35",
152 "36",
153 "37",
154 "38",
155 "39",
156 "Disconnect-Request", //!< 40
157 "Disconnect-ACK",
158 "Disconnect-NAK",
159 "CoA-Request",
160 "CoA-ACK",
161 "CoA-NAK",
162 "46",
163 "47",
164 "48",
165 "49",
166 "IP-Address-Allocate", //!< 50
167 "IP-Address-Release",
168 "Protocol-Error",
169};
170
171
172/** If we get a reply, the request must come from one of a small
173 * number of packet types.
174 */
190
193
194static int dict_flag_encrypt(fr_dict_attr_t **da_p, char const *value, UNUSED fr_dict_flag_parser_rule_t const *rules)
195{
196 static fr_table_num_sorted_t const encrypted[] = {
197 { L("Ascend-Secret"), RADIUS_FLAG_ENCRYPT_ASCEND_SECRET },
198 { L("Tunnel-Password"), RADIUS_FLAG_ENCRYPT_TUNNEL_PASSWORD },
199 { L("User-Password"), RADIUS_FLAG_ENCRYPT_USER_PASSWORD}
200 };
201 static size_t encrypted_len = NUM_ELEMENTS(encrypted);
202
205
207 if (encrypt == RADIUS_FLAG_ENCRYPT_INVALID) {
208 fr_strerror_printf("Unknown encryption type '%s'", value);
209 return -1;
210 }
211
212 flags->encrypt = encrypt;
213
214 return 0;
215}
216
220
222 { L("abinary"), { .func = dict_flag_abinary } },
223 { L("concat"), { .func = dict_flag_concat } },
224 { L("encrypt"), { .func = dict_flag_encrypt, .needs_value = true } },
225 { L("extended"), { .func = dict_flag_extended } },
226 { L("has_tag"), { .func = dict_flag_has_tag } },
227 { L("long_extended"), { .func = dict_flag_long_extended } }
228};
229
231{
232 int i;
233
234 if ((code == FR_RADIUS_CODE_UNDEFINED) || (code >= FR_RADIUS_CODE_MAX)) return -1;
235
236 for (i = 1; i < FR_RADIUS_CODE_MAX; i++) {
237 allowed[i] |= (allowed_replies[i] == code);
238 }
239
240 return 0;
241}
242
243/** Do Ascend-Send / Recv-Secret calculation.
244 *
245 * The secret is hidden by xoring with a MD5 digest created from
246 * the RADIUS shared secret and the authentication vector.
247 * We put them into MD5 in the reverse order from that used when
248 * encrypting passwords to RADIUS.
249 */
251 char const *secret, size_t secret_len, uint8_t const *vector)
252{
253 fr_md5_ctx_t *md5_ctx;
254 size_t i;
256 fr_dbuff_t work_dbuff = FR_DBUFF(dbuff);
257
258 FR_DBUFF_EXTEND_LOWAT_OR_RETURN(&work_dbuff, sizeof(digest));
259
260 md5_ctx = fr_md5_ctx_alloc_from_list();
262 fr_md5_update(md5_ctx, (uint8_t const *) secret, secret_len);
263 fr_md5_final(digest, md5_ctx);
265
266 if (inlen > sizeof(digest)) inlen = sizeof(digest);
267 for (i = 0; i < inlen; i++) digest[i] ^= in[i];
268
269 fr_dbuff_in_memcpy(&work_dbuff, digest, sizeof(digest));
270
271 return fr_dbuff_set(dbuff, &work_dbuff);
272}
273
274/** Basic validation of RADIUS packet header
275 *
276 * @note fr_strerror errors are only available if fr_debug_lvl > 0. This is to reduce CPU time
277 * consumed when discarding malformed packet.
278 *
279 * @param[in] sockfd we're reading from.
280 * @param[out] src_ipaddr of the packet.
281 * @param[out] src_port of the packet.
282 * @param[out] code Pointer to where to write the packet code.
283 * @return
284 * - -1 on failure.
285 * - 1 on decode error.
286 * - >= RADIUS_HEADER_LENGTH on success. This is the packet length as specified in the header.
287 */
288ssize_t fr_radius_recv_header(int sockfd, fr_ipaddr_t *src_ipaddr, uint16_t *src_port, unsigned int *code)
289{
290 ssize_t data_len, packet_len;
291 uint8_t header[4];
292
293 data_len = udp_recv_peek(sockfd, header, sizeof(header), UDP_FLAGS_PEEK, src_ipaddr, src_port);
294 if (data_len < 0) {
295 if ((errno == EAGAIN) || (errno == EINTR)) return 0;
296 return -1;
297 }
298
299 /*
300 * Too little data is available, discard the packet.
301 */
302 if (data_len < 4) {
303 char buffer[INET6_ADDRSTRLEN];
304
305 FR_DEBUG_STRERROR_PRINTF("Expected at least 4 bytes of header data, got %zd bytes", data_len);
306invalid:
307 FR_DEBUG_STRERROR_PRINTF_PUSH("Invalid data from %s",
308 inet_ntop(src_ipaddr->af, &src_ipaddr->addr, buffer, sizeof(buffer)));
309 (void) udp_recv_discard(sockfd);
310
311 return 0;
312 }
313
314 /*
315 * See how long the packet says it is.
316 */
317 packet_len = (header[2] * 256) + header[3];
318
319 /*
320 * The length in the packet says it's less than
321 * a RADIUS header length: discard it.
322 */
323 if (packet_len < RADIUS_HEADER_LENGTH) {
324 FR_DEBUG_STRERROR_PRINTF("Expected at least " STRINGIFY(RADIUS_HEADER_LENGTH) " bytes of packet "
325 "data, got %zd bytes", packet_len);
326 goto invalid;
327 }
328
329 /*
330 * Enforce RFC requirements, for sanity.
331 * Anything after 4k will be discarded.
332 */
333 if (packet_len > MAX_PACKET_LEN) {
334 FR_DEBUG_STRERROR_PRINTF("Length field value too large, expected maximum of "
335 STRINGIFY(MAX_PACKET_LEN) " bytes, got %zd bytes", packet_len);
336 goto invalid;
337 }
338
339 *code = header[0];
340
341 /*
342 * The packet says it's this long, but the actual UDP
343 * size could still be smaller.
344 */
345 return packet_len;
346}
347
348/** Sign a previously encoded packet
349 *
350 * Calculates the request/response authenticator for packets which need it, and fills
351 * in the message-authenticator value if the attribute is present in the encoded packet.
352 *
353 * @param[in,out] packet (request or response).
354 * @param[in] vector original packet vector to use
355 * @param[in] secret to sign the packet with.
356 * @param[in] secret_len The length of the secret.
357 * @return
358 * - <0 on error
359 * - 0 on success
360 */
361int fr_radius_sign(uint8_t *packet, uint8_t const *vector,
362 uint8_t const *secret, size_t secret_len)
363{
364 uint8_t *msg, *end;
365 size_t packet_len = fr_nbo_to_uint16(packet + 2);
366
367 /*
368 * No real limit on secret length, this is just
369 * to catch uninitialised fields.
370 */
371 if (!fr_cond_assert(secret_len <= UINT16_MAX)) {
372 fr_strerror_printf("Secret is too long. Expected <= %u, got %zu",
373 (unsigned int) UINT16_MAX, secret_len);
374 return -1;
375 }
376
377 if (packet_len < RADIUS_HEADER_LENGTH) {
378 fr_strerror_const("Packet must be encoded before calling fr_radius_sign()");
379 return -1;
380 }
381
382 /*
383 * Find Message-Authenticator. Its value has to be
384 * calculated before we calculate the Request
385 * Authenticator or the Response Authenticator.
386 */
387 msg = packet + RADIUS_HEADER_LENGTH;
388 end = packet + packet_len;
389
390 while (msg < end) {
391 if ((end - msg) < 2) goto invalid_attribute;
392
393 if (msg[0] != FR_MESSAGE_AUTHENTICATOR) {
394 if (msg[1] < 2) goto invalid_attribute;
395
396 if ((msg + msg[1]) > end) {
397 invalid_attribute:
398 fr_strerror_printf("Invalid attribute at offset %zd", msg - packet);
399 return -1;
400 }
401 msg += msg[1];
402 continue;
403 }
404
405 if (msg[1] < 18) {
406 fr_strerror_const("Message-Authenticator is too small");
407 return -1;
408 }
409
410 switch (packet[0]) {
414 memset(packet + 4, 0, RADIUS_AUTH_VECTOR_LENGTH);
415 break;
416
426 if (!vector) goto need_original;
427 memcpy(packet + 4, vector, RADIUS_AUTH_VECTOR_LENGTH);
428 break;
429
432 /* packet + 4 MUST be the Request Authenticator filled with random data */
433 break;
434
435 default:
436 goto bad_packet;
437 }
438
439 /*
440 * Force Message-Authenticator to be zero,
441 * calculate the HMAC, and put it into the
442 * Message-Authenticator attribute.
443 */
444 memset(msg + 2, 0, RADIUS_AUTH_VECTOR_LENGTH);
445 fr_hmac_md5(msg + 2, packet, packet_len, secret, secret_len);
446 break;
447 }
448
449 /*
450 * Initialize the request authenticator.
451 */
452 switch (packet[0]) {
456 memset(packet + 4, 0, RADIUS_AUTH_VECTOR_LENGTH);
457 break;
458
468 if (!vector) {
469 need_original:
470 fr_strerror_const("Cannot sign response packet without a request packet");
471 return -1;
472 }
473 memcpy(packet + 4, vector, RADIUS_AUTH_VECTOR_LENGTH);
474 break;
475
476 /*
477 * The Request Authenticator is random numbers.
478 * We don't need to sign anything else, so
479 * return.
480 */
483 return 0;
484
485 default:
486 bad_packet:
487 fr_strerror_printf("Cannot sign unknown packet code %u", packet[0]);
488 return -1;
489 }
490
491 /*
492 * Request / Response Authenticator = MD5(packet + secret)
493 */
494 {
495 fr_md5_ctx_t *md5_ctx;
496
497 md5_ctx = fr_md5_ctx_alloc_from_list();
498 fr_md5_update(md5_ctx, packet, packet_len);
499 fr_md5_update(md5_ctx, secret, secret_len);
500 fr_md5_final(packet + 4, md5_ctx);
502 }
503
504 return 0;
505}
506
508 [FR_RADIUS_FAIL_NONE] = "none",
509 [FR_RADIUS_FAIL_MIN_LENGTH_PACKET] = "packet is smaller than the minimum packet length",
510 [FR_RADIUS_FAIL_MAX_LENGTH_PACKET] = "packet is larger than the maximum packet length",
511 [FR_RADIUS_FAIL_MIN_LENGTH_FIELD] = "header 'length' field has a value smaller than the minimum packet length",
512 [FR_RADIUS_FAIL_MIN_LENGTH_MISMATCH] = "header 'length' field has a value larger than the received data",
513 [FR_RADIUS_FAIL_UNKNOWN_PACKET_CODE] = "unknown packet code",
514 [FR_RADIUS_FAIL_UNEXPECTED_REQUEST_CODE] = "unexpected request code",
515 [FR_RADIUS_FAIL_UNEXPECTED_RESPONSE_CODE] = "unexpected response code",
516 [FR_RADIUS_FAIL_TOO_MANY_ATTRIBUTES] = "packet contains too many attributes",
517
518 [FR_RADIUS_FAIL_INVALID_ATTRIBUTE] = "attribute number 0 is invalid",
519
520 [FR_RADIUS_FAIL_HEADER_OVERFLOW] = "attribute header overflows the packet",
521 [FR_RADIUS_FAIL_ATTRIBUTE_TOO_SHORT] = "attribute 'length' field contains invalid value",
522 [FR_RADIUS_FAIL_ATTRIBUTE_OVERFLOW] = "attribute 'length' field overflows the packet",
523 [FR_RADIUS_FAIL_ATTRIBUTE_DECODE] = "unable to decode attributes",
524
525 [FR_RADIUS_FAIL_MA_INVALID_LENGTH] = "Message-Authenticator has invalid length",
526 [FR_RADIUS_FAIL_MA_MISSING] = "Message-Authenticator is required for this packet, but it is missing",
527 [FR_RADIUS_FAIL_MA_INVALID] = "Message-Authenticator fails verification. shared secret is incorrect",
528 [FR_RADIUS_FAIL_MA_TOO_MANY] = "More than one Message-Authenticator in a packet is invalid",
529 [FR_RADIUS_FAIL_PROXY_STATE_MISSING_MA] = "The packet contains Proxy-State, but no Message-Authenticator",
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, or 0 for "fatal error"
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 *packet_len_p = 0; /* also signals a fatal error */
559
560 /*
561 * Check for packets smaller than the packet header.
562 *
563 * RFC 2865, Section 3., subsection 'length' says:
564 *
565 * "The minimum length is 20 ..."
566 */
567 if (packet_len < RADIUS_HEADER_LENGTH) {
569 goto finish;
570 }
571
572 /*
573 * Check for packets with mismatched size.
574 * i.e. We've received 128 bytes, and the packet header
575 * says it's 256 bytes long.
576 */
577 totallen = fr_nbo_to_uint16(packet + 2);
578
579 /*
580 * Repeat the length checks. This time, instead of
581 * looking at the data we received, look at the value
582 * of the 'length' field inside of the packet.
583 *
584 * Check for packets smaller than the packet header.
585 *
586 * RFC 2865, Section 3., subsection 'length' says:
587 *
588 * "The minimum length is 20 ..."
589 */
590 if (totallen < RADIUS_HEADER_LENGTH) {
592 goto finish;
593 }
594
595 /*
596 * And again, for the value of the 'length' field.
597 *
598 * RFC 2865, Section 3., subsection 'length' says:
599 *
600 * " ... and maximum length is 4096."
601 *
602 * HOWEVER. This requirement is for the network layer.
603 * If the code gets here, we assume that a well-formed
604 * packet is an OK packet.
605 *
606 * We allow both the UDP data length, and the RADIUS
607 * "length" field to contain up to 64K of data.
608 */
609
610 /*
611 * RFC 2865, Section 3., subsection 'length' says:
612 *
613 * "If the packet is shorter than the Length field
614 * indicates, it MUST be silently discarded."
615 *
616 * i.e. No response to the NAS.
617 */
618 if (totallen > packet_len) {
620 *packet_len_p = totallen;
621 goto finish;
622 }
623
624 /*
625 * RFC 2865, Section 3., subsection 'length' says:
626 *
627 * "Octets outside the range of the Length field MUST be
628 * treated as padding and ignored on reception."
629 */
630 *packet_len_p = packet_len = totallen;
631
632 /*
633 * Code of 0 is not understood.
634 * Code of 16 or greater is not understood.
635 */
636 if ((packet[0] == 0) ||
637 (packet[0] >= FR_RADIUS_CODE_MAX)) {
639 goto finish;
640 }
641
642 switch (packet[0]) {
643 /*
644 * Message-Authenticator is required in Status-Server
645 * packets, otherwise they can be trivially forged.
646 */
648 require_message_authenticator = true;
649 break;
650
651 /*
652 * Message-Authenticator may or may not be
653 * required for Access-* packets.
654 */
660 break;
661
662 /*
663 * Message-Authenticator is not required for all other packets, but is required if the
664 * caller asks for it.
665 */
668
672
676 break;
677
678 /*
679 * All other packet codes are not handled by the encoder, so we reject them.
680 */
681 default:
683 goto finish;
684 }
685
686 /*
687 * Walk through the packet's attributes, ensuring that
688 * they add up EXACTLY to the size of the packet.
689 *
690 * If they don't, then the attributes either under-fill
691 * or over-fill the packet. Any parsing of the packet
692 * is impossible, and will result in unknown side effects.
693 *
694 * This would ONLY happen with buggy RADIUS implementations,
695 * or with an intentional attack. Either way, we do NOT want
696 * to be vulnerable to this problem.
697 */
698 attr = packet + RADIUS_HEADER_LENGTH;
699 end = packet + packet_len;
700 num_attributes = 0;
701
702 while (attr < end) {
703 /*
704 * We need at least 2 bytes to check the
705 * attribute header.
706 */
707 if ((end - attr) < 2) {
709 goto fatal;
710 }
711
712 /*
713 * Attribute number zero is NOT defined.
714 */
715 if (attr[0] == 0) {
717 goto fatal;
718 }
719
720 /*
721 * Attributes are at LEAST as long as the ID & length
722 * fields. Anything shorter is an invalid attribute.
723 */
724 if (attr[1] < 2) {
726 goto fatal;
727 }
728
729 /*
730 * If there are fewer bytes in the packet than in the
731 * attribute, it's a bad packet.
732 */
733 if ((attr + attr[1]) > end) {
735 goto fatal;
736 }
737
738 /*
739 * Sanity check the attributes for length.
740 */
741 switch (attr[0]) {
742 default: /* don't do anything by default */
743 break;
744
745 /*
746 * If there's an EAP-Message, we require
747 * a Message-Authenticator.
748 */
749 case FR_EAP_MESSAGE:
750 require_message_authenticator = true;
751 break;
752
753 case FR_MESSAGE_AUTHENTICATOR:
754 if (attr[1] != 2 + RADIUS_AUTH_VECTOR_LENGTH) {
756 *packet_len_p = 0;
757 goto finish;
758 }
759
760 /*
761 * Can't have two of them.
762 */
763 if (seen_ma) {
765 *packet_len_p = 0;
766 goto finish;
767 }
768
769 seen_ma = true;
770 break;
771 }
772
773 attr += attr[1];
774 num_attributes++; /* seen one more attribute */
775
776 /*
777 * If we're configured to look for a maximum number of
778 * attributes, and we've seen more than that maximum,
779 * then throw the packet away, as a possible DoS.
780 */
781 if (num_attributes > max_attributes) {
783 /* don't set *packet_len_p=0, we just discard _this_ packet */
784 goto finish;
785 }
786 }
787
788 /*
789 * http://www.freeradius.org/rfc/rfc2869.html#EAP-Message
790 *
791 * A packet with an EAP-Message attribute MUST also have
792 * a Message-Authenticator attribute.
793 *
794 * A Message-Authenticator all by itself is OK, though.
795 *
796 * Similarly, Status-Server packets MUST contain
797 * Message-Authenticator attributes.
798 */
799 if (require_message_authenticator && !seen_ma) {
801fatal:
802 *packet_len_p = 0;
803 goto finish;
804 }
805
806finish:
807
808 if (reason) *reason = failure;
809
810 return (failure == FR_RADIUS_FAIL_NONE);
811}
812
813
814/** Verify the signature of a request / response packet
815 *
816 * This function does its work by calling fr_radius_sign(), and then comparing the signature in the packet
817 * with the one we calculated. If they differ, there's a problem.
818 *
819 * @note - We rely on the security of the shared secret for UDP and TCP transport. For TLS transport, we
820 * rely on TLS to make the RADIUS packets both secure and private.
821 *
822 * @note - The BlastRADIUS mitigations require that "require_message_authenticator" and "limit_proxy_state"
823 * are used only for Access-Request packets. These mitigations MUST NOT be used for any any other packet
824 * codes.
825 *
826 * The caller should have called fr_radius_packet_ok() to see if the packet is well-formed, at least for the
827 * base RFC attributes.
828 *
829 * @param[in] packet the raw RADIUS packet (request or response)
830 * @param[in] vector the original packet vector
831 * @param[in] secret the shared secret
832 * @param[in] secret_len the length of the secret
833 * @param[in] require_message_authenticator whether we require Message-Authenticator.
834 * @param[in] limit_proxy_state whether we allow Proxy-State without Message-Authenticator.
835 * @return
836 * < <0 on error (negative fr_radius_decode_fail_t)
837 * - 0 on success.
838 */
839int fr_radius_verify(uint8_t *packet, uint8_t const *vector,
840 uint8_t const *secret, size_t secret_len,
841 bool require_message_authenticator, bool limit_proxy_state)
842{
843 bool found_message_authenticator = false;
844 bool found_proxy_state = false;
845 int rcode;
846 int code;
847 uint8_t *attr, *msg, *end;
848 size_t packet_len = fr_nbo_to_uint16(packet + 2);
849 uint8_t request_authenticator[RADIUS_AUTH_VECTOR_LENGTH];
850 uint8_t message_authenticator[RADIUS_AUTH_VECTOR_LENGTH];
851
852 if (packet_len < RADIUS_HEADER_LENGTH) {
853 fr_strerror_printf("invalid packet length %zu", packet_len);
855 }
856
857 code = packet[0];
858 if (!code || (code >= FR_RADIUS_CODE_MAX)) {
859 fr_strerror_printf("Unknown reply code %d", code);
861 }
862
863 /*
864 * RFC 5997 says that all Status-Server packets MUST contain Message-Authenticator.
865 */
866 require_message_authenticator |= (code == FR_RADIUS_CODE_STATUS_SERVER);
867
868 memcpy(request_authenticator, packet + 4, sizeof(request_authenticator));
869
870 /*
871 * Find Message-Authenticator. Its value has to be
872 * calculated before we calculate the Request
873 * Authenticator or the Response Authenticator.
874 */
875 msg = NULL;
876 attr = packet + RADIUS_HEADER_LENGTH;
877 end = packet + packet_len;
878
879 /*
880 * See what we need to do in order to verify the packet.
881 *
882 * Note that fr_radius_packet_ok() also does these checks, but it's worth re-doing them here so
883 * that potential API mis-use is safe. i.e. we do "defense in depth".
884 */
885 while (attr < end) {
886 if ((end - attr) < 2) goto invalid_attribute;
887
888 if (attr[1] < 2) goto invalid_attribute;
889
890 if ((attr + attr[1]) > end) {
891 invalid_attribute:
892 fr_strerror_printf("invalid attribute at offset %zd", attr - packet);
894 }
895
896 /*
897 * If there's no Message-Authenticator, then we need to check Proxy-State, but only if
898 * the caller asked us to limit Proxy-State.
899 */
900 if (attr[0] == FR_PROXY_STATE) {
901 found_proxy_state = limit_proxy_state;
902 goto next;
903 }
904
905 /*
906 * Check the contents of Message-Authenticator
907 */
908 if (attr[0] == FR_MESSAGE_AUTHENTICATOR) {
909 if (found_message_authenticator) {
910 fr_strerror_const("Multiple Message-Authenticators are invalid");
912 }
913
914
915 if (attr[1] != 18) {
916 fr_strerror_const("too small Message-Authenticator");
918 }
919
920 /*
921 * If we have found Message-Authenticator, then we verify that. We also can stop
922 * processing the packet, as we don't care about the contents of Proxy-State.
923 */
924 memcpy(message_authenticator, attr + 2, sizeof(message_authenticator));
925 found_message_authenticator = true;
926 msg = attr;
927 goto next;
928 }
929
930 /*
931 * RFC 3579 Section 3.1 requires Message-Authenticator if the packet contains
932 * EAP-Message.
933 */
934 if (attr[0] == FR_EAP_MESSAGE) require_message_authenticator = true;
935
936 next:
937 attr += attr[1];
938 }
939
940 /*
941 * Enforce limit_proxy_state for Access-Request packets.
942 */
943 if (code == FR_RADIUS_CODE_ACCESS_REQUEST) {
944 if (limit_proxy_state && found_proxy_state && !found_message_authenticator) {
945 fr_strerror_const("Proxy-State is not allowed without Message-Authenticator");
947 }
948 }
949
950 /*
951 * The require_message_authenticator flag can be set for any of these packet types. For other
952 * packet types, we check it if it exists, but we allow packets to not contain it.
953 */
954 if (require_message_authenticator && !found_message_authenticator) {
955 fr_strerror_printf("%s is missing the required Message-Authenticator attribute",
958 }
959
960 /*
961 * Overwrite the contents of Message-Authenticator
962 * with the one we calculate.
963 */
964 rcode = fr_radius_sign(packet, vector, secret, secret_len);
965 if (rcode < 0) {
966 fr_strerror_const_push("Failed calculating correct authenticator");
967 return -FR_RADIUS_FAIL_VERIFY;
968 }
969
970 /*
971 * Check the Message-Authenticator first.
972 *
973 * If it's invalid, restore the original
974 * Message-Authenticator and Request Authenticator
975 * fields.
976 *
977 * If it's valid the original and calculated
978 * message authenticators are the same, so we don't
979 * need to do anything.
980 */
981 if (msg &&
982 (fr_digest_cmp(message_authenticator, msg + 2, sizeof(message_authenticator)) != 0)) {
983 memcpy(msg + 2, message_authenticator, sizeof(message_authenticator));
984 memcpy(packet + 4, request_authenticator, sizeof(request_authenticator));
985
986 fr_strerror_const("invalid Message-Authenticator (shared secret is incorrect)");
988 }
989
990 /*
991 * These are random numbers, so there's no point in
992 * comparing them.
993 */
995 return 0;
996 }
997
998 /*
999 * Check the Request Authenticator.
1000 */
1001 if (fr_digest_cmp(request_authenticator, packet + 4, sizeof(request_authenticator)) != 0) {
1002 memcpy(packet + 4, request_authenticator, sizeof(request_authenticator));
1003 if (vector) {
1004 fr_strerror_const("invalid Response Authenticator (shared secret is incorrect)");
1005 } else {
1006 fr_strerror_const("invalid Request Authenticator (shared secret is incorrect)");
1007 }
1008 return -FR_RADIUS_FAIL_VERIFY;
1009 }
1010
1011 return 0;
1012}
1013
1014void *fr_radius_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx);
1015
1016void *fr_radius_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
1017{
1018 fr_pair_t *c = current;
1019 fr_dict_t *dict = talloc_get_type_abort(uctx, fr_dict_t);
1020
1021 while ((c = fr_dcursor_list_next(cursor, c))) {
1022 PAIR_VERIFY(c);
1023 if ((c->da->dict == dict) &&
1024 (!c->da->flags.internal || ((c->da->attr > FR_TAG_BASE) && (c->da->attr < (FR_TAG_BASE + 0x20))))) {
1025 break;
1026 }
1027 }
1028
1029 return c;
1030}
1031
1032
1034{
1035 ssize_t slen;
1036 fr_pair_t const *vp;
1037 fr_dcursor_t cursor;
1038 fr_dbuff_t work_dbuff, length_dbuff;
1039
1040 /*
1041 * The RADIUS header can't do more than 64K of data.
1042 */
1043 work_dbuff = FR_DBUFF_MAX(dbuff, 65535);
1044
1045 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, packet_ctx->code, packet_ctx->id);
1046 length_dbuff = FR_DBUFF(&work_dbuff);
1048
1049 switch (packet_ctx->code) {
1052 packet_ctx->request_authenticator = fr_dbuff_current(&work_dbuff);
1053
1054 /*
1055 * Allow over-rides of the authentication vector for testing.
1056 */
1058 if (vp && (vp->vp_length >= RADIUS_AUTH_VECTOR_LENGTH)) {
1059 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, vp->vp_octets, RADIUS_AUTH_VECTOR_LENGTH);
1060 } else {
1061 int i;
1062
1063 for (i = 0; i < 4; i++) {
1064 FR_DBUFF_IN_RETURN(&work_dbuff, (uint32_t) fr_rand());
1065 }
1066 }
1067 break;
1068
1078 if (!packet_ctx->request_authenticator) {
1079 fr_strerror_const("Cannot encode response without request");
1080 return -1;
1081 }
1082 FR_DBUFF_IN_MEMCPY_RETURN(&work_dbuff, packet_ctx->request_authenticator, RADIUS_AUTH_VECTOR_LENGTH);
1083 break;
1084
1087 /*
1088 * Tunnel-Password encoded attributes are allowed
1089 * in CoA-Request packets, by RFC 5176 Section
1090 * 3.6. HOWEVER, the tunnel passwords are
1091 * "encrypted" using the Request Authenticator,
1092 * which is all zeros! That makes them much
1093 * easier to decrypt. The only solution here is
1094 * to say "don't do that!"
1095 */
1097 packet_ctx->request_authenticator = fr_dbuff_current(&work_dbuff);
1098
1100 break;
1101
1102 default:
1103 fr_strerror_printf("Cannot encode unknown packet code %d", packet_ctx->code);
1104 return -1;
1105 }
1106
1107 /*
1108 * Always add Message-Authenticator after the packet
1109 * header for insecure transport protocols.
1110 */
1111 if (!packet_ctx->common->secure_transport) switch (packet_ctx->code) {
1115#ifdef NAS_VIOLATES_RFC
1116 /*
1117 * Allow ridiculous behavior for vendors who violate the RFCs.
1118 *
1119 * But only if there's no EAP-Message in the packet.
1120 */
1121 if (packet_ctx->allow_vulnerable_clients && !fr_pair_find_by_da(vps, NULL, attr_eap_message)) {
1122 break;
1123 }
1125#endif
1126
1130 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, FR_MESSAGE_AUTHENTICATOR, 0x12,
1131 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1132 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
1133 packet_ctx->seen_message_authenticator = true;
1134 break;
1135
1136 default:
1137 break;
1138 }
1139
1140 /*
1141 * If we're sending Protocol-Error, add in
1142 * Original-Packet-Code manually. If the user adds it
1143 * later themselves, well, too bad.
1144 */
1146 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, FR_EXTENDED_ATTRIBUTE_1, 0x07, 0x04 /* Original-Packet-Code */,
1147 0x00, 0x00, 0x00, packet_ctx->request_code);
1148 }
1149
1150 /*
1151 * Loop over the reply attributes for the packet.
1152 */
1154 while ((vp = fr_dcursor_current(&cursor))) {
1155 PAIR_VERIFY(vp);
1156
1157 /*
1158 * Encode an individual VP
1159 */
1160 slen = fr_radius_encode_pair(&work_dbuff, &cursor, packet_ctx);
1161 if (slen < 0) return slen;
1162 } /* done looping over all attributes */
1163
1164 /*
1165 * Add Proxy-State to the end of the packet if the caller requested it.
1166 */
1167 if (packet_ctx->add_proxy_state) {
1168 FR_DBUFF_IN_BYTES_RETURN(&work_dbuff, FR_PROXY_STATE, (uint8_t) (2 + sizeof(packet_ctx->common->proxy_state)));
1169 FR_DBUFF_IN_RETURN(&work_dbuff, packet_ctx->common->proxy_state);
1170 }
1171
1172 /*
1173 * Fill in the length field we zeroed out earlier.
1174 *
1175 */
1176 fr_dbuff_in(&length_dbuff, (uint16_t) (fr_dbuff_used(&work_dbuff)));
1177
1178 FR_PROTO_HEX_DUMP(fr_dbuff_start(&work_dbuff), fr_dbuff_used(&work_dbuff), "%s encoded packet", __FUNCTION__);
1179
1180 return fr_dbuff_set(dbuff, &work_dbuff);
1181}
1182
1184 uint8_t *packet, size_t packet_len,
1185 fr_radius_decode_ctx_t *decode_ctx)
1186{
1187 ssize_t slen;
1188 uint8_t const *attr, *end;
1189 static const uint8_t zeros[RADIUS_AUTH_VECTOR_LENGTH] = {};
1190
1191 decode_ctx->reason = FR_RADIUS_FAIL_NONE;
1192
1193 if (!decode_ctx->request_authenticator) {
1194 switch (packet[0]) {
1197 decode_ctx->request_authenticator = packet + 4;
1198 break;
1199
1203 decode_ctx->request_authenticator = zeros;
1204 break;
1205
1206 default:
1207 fr_strerror_const("No authentication vector passed for packet decode");
1209 return -1;
1210 }
1211 }
1212
1213 if (decode_ctx->request_code) {
1214 unsigned int code = packet[0];
1215
1216 if (code >= FR_RADIUS_CODE_MAX) {
1218 return -1;
1219 }
1220 if (decode_ctx->request_code >= FR_RADIUS_CODE_MAX) {
1222 return -1;
1223 }
1224
1225 if (!allowed_replies[code]) {
1227 return -1;
1228 }
1229
1230 /*
1231 * Protocol error can reply to any packet.
1232 *
1233 * Status-Server can get any reply.
1234 *
1235 * Otherwise the reply code must be associated with the request code we sent.
1236 */
1237 if ((allowed_replies[code] != decode_ctx->request_code) &&
1239 (decode_ctx->request_code != FR_RADIUS_CODE_STATUS_SERVER)) {
1241 return -1;
1242 }
1243 }
1244
1245 /*
1246 * We can skip verification for dynamic client checks, and where packets are unsigned as with
1247 * RADIUS/1.1.
1248 */
1249 if (decode_ctx->verify) {
1250 if (!decode_ctx->request_authenticator) decode_ctx->request_authenticator = zeros;
1251
1252 if (fr_radius_verify(packet, decode_ctx->request_authenticator,
1253 (uint8_t const *) decode_ctx->common->secret, decode_ctx->common->secret_length,
1254 decode_ctx->require_message_authenticator, decode_ctx->limit_proxy_state) < 0) {
1255 decode_ctx->reason = FR_RADIUS_FAIL_VERIFY;
1256 return -1;
1257 }
1258 }
1259
1260 attr = packet + 20;
1261 end = packet + packet_len;
1262
1263 /*
1264 * The caller MUST have called fr_radius_ok() first. If
1265 * he doesn't, all hell breaks loose.
1266 */
1267 while (attr < end) {
1268 slen = fr_radius_decode_pair(ctx, out, attr, (end - attr), decode_ctx);
1269 if (slen < 0) {
1271 return slen;
1272 }
1273
1274 /*
1275 * If slen is larger than the room in the packet,
1276 * all kinds of bad things happen.
1277 */
1278 if (!fr_cond_assert(slen <= (end - attr))) {
1279 return -slen;
1280 }
1281
1282 attr += slen;
1283 talloc_free_children(decode_ctx->tmp_ctx);
1284 }
1285
1286 /*
1287 * We've parsed the whole packet, return that.
1288 */
1289 return packet_len;
1290}
1291
1292/** Simple wrapper for callers who just need a shared secret
1293 *
1294 * @note - All callers verify the packet via fr_radius_packet_verify() before calling this function.
1295 */
1297 uint8_t *packet, size_t packet_len,
1298 uint8_t const *vector, char const *secret)
1299{
1300 ssize_t rcode;
1301 fr_radius_ctx_t common_ctx = {};
1303
1304 common_ctx.secret = secret;
1305 common_ctx.secret_length = strlen(secret);
1306
1307 packet_ctx.common = &common_ctx;
1308 packet_ctx.tmp_ctx = talloc(ctx, uint8_t);
1309 packet_ctx.request_authenticator = vector;
1310 packet_ctx.end = packet + packet_len;
1311
1312 rcode = fr_radius_decode(ctx, out, packet, packet_len, &packet_ctx);
1313 talloc_free(packet_ctx.tmp_ctx);
1314
1315 return rcode;
1316}
1317
1319{
1320 if (instance_count > 0) {
1322 return 0;
1323 }
1324
1326
1328 fail:
1330 return -1;
1331 }
1332
1335 goto fail;
1336 }
1337
1338 instantiated = true;
1339 return 0;
1340}
1341
1343{
1344 if (!instantiated) return;
1345
1346 if (--instance_count != 0) return;
1347
1349
1350 instantiated = false;
1351}
1352
1354{
1356
1357 if (da->parent->type == FR_TYPE_STRUCT) {
1358 if (flags->extended) {
1359 fr_strerror_const("Attributes with 'extended' flag cannot be used inside of a 'struct'");
1360 return false;
1361 }
1362
1363 if (flags->long_extended) {
1364 fr_strerror_const("Attributes with 'long_extended' flag cannot be used inside of a 'struct'");
1365 return false;
1366 }
1367
1368
1369 if (flags->concat) {
1370 fr_strerror_const("Attributes with 'concat' flag cannot be used inside of a 'struct'");
1371 return false;
1372 }
1373
1374 if (flags->has_tag) {
1375 fr_strerror_const("Attributes with 'tag' flag cannot be used inside of a 'struct'");
1376 return false;
1377 }
1378
1379 if (flags->abinary) {
1380 fr_strerror_const("Attributes with 'abinary' flag cannot be used inside of a 'struct'");
1381 return false;
1382 }
1383
1384 if (flags->encrypt > 0) {
1385 fr_strerror_const("Attributes with 'encrypt' flag cannot be used inside of a 'struct'");
1386 return false;
1387 }
1388
1389 return true;
1390 }
1391
1392 if (da->flags.length > 253) {
1393 fr_strerror_printf("Attributes cannot be more than 253 octets in length");
1394 return false;
1395 }
1396 /*
1397 * Secret things are secret.
1398 */
1399 if (flags->encrypt != 0) da->flags.secret = true;
1400
1401 if (flags->concat) {
1402 if (!da->parent->flags.is_root) {
1403 fr_strerror_const("Attributes with the 'concat' flag MUST be at the root of the dictionary");
1404 return false;
1405 }
1406
1407 if (da->type != FR_TYPE_OCTETS) {
1408 fr_strerror_const("Attributes with the 'concat' flag MUST be of data type 'octets'");
1409 return false;
1410 }
1411
1412 return true; /* can't use any other flag */
1413 }
1414
1415 /*
1416 * Tagged attributes can only be of two data types. They
1417 * can, however, be VSAs.
1418 */
1419 if (flags->has_tag) {
1420 if ((da->type != FR_TYPE_UINT32) && (da->type != FR_TYPE_STRING)) {
1421 fr_strerror_printf("The 'has_tag' flag can only be used for attributes of type 'integer' "
1422 "or 'string'");
1423 return false;
1424 }
1425
1426 if (!(da->parent->flags.is_root ||
1427 ((da->parent->type == FR_TYPE_VENDOR) &&
1428 (da->parent->parent && da->parent->parent->type == FR_TYPE_VSA)))) {
1429 fr_strerror_const("The 'has_tag' flag can only be used with RFC and VSA attributes");
1430 return false;
1431 }
1432
1433 return true;
1434 }
1435
1436 if (flags->extended) {
1437 if (da->type != FR_TYPE_TLV) {
1438 fr_strerror_const("The 'long' or 'extended' flag can only be used for attributes of type 'tlv'");
1439 return false;
1440 }
1441
1442 if (!da->parent->flags.is_root) {
1443 fr_strerror_const("The 'long' flag can only be used for top-level RFC attributes");
1444 return false;
1445 }
1446
1447 return true;
1448 }
1449
1450 /*
1451 * Stupid hacks for MS-CHAP-MPPE-Keys. The User-Password
1452 * encryption method has no provisions for encoding the
1453 * length of the data. For User-Password, the data is
1454 * (presumably) all printable non-zero data. For
1455 * MS-CHAP-MPPE-Keys, the data is binary crap. So... we
1456 * MUST specify a length in the dictionary.
1457 */
1458 if ((flags->encrypt == RADIUS_FLAG_ENCRYPT_USER_PASSWORD) && (da->type != FR_TYPE_STRING)) {
1459 if (da->type != FR_TYPE_OCTETS) {
1460 fr_strerror_printf("The 'encrypt=User-Password' flag can only be used with "
1461 "attributes of type 'string'");
1462 return false;
1463 }
1464
1465 if (da->flags.length == 0) {
1466 fr_strerror_printf("The 'encrypt=User-Password' flag MUST be used with an explicit length for "
1467 "'octets' data types");
1468 return false;
1469 }
1470 }
1471
1472 switch (da->type) {
1473 case FR_TYPE_STRING:
1474 break;
1475
1476 case FR_TYPE_TLV:
1477 case FR_TYPE_IPV4_ADDR:
1478 case FR_TYPE_UINT32:
1479 case FR_TYPE_OCTETS:
1480 if (flags->encrypt != RADIUS_FLAG_ENCRYPT_ASCEND_SECRET) break;
1482
1483 default:
1484 if (flags->encrypt) {
1485 fr_strerror_printf("The 'encrypt' flag cannot be used with attributes of type '%s'",
1486 fr_type_to_str(da->type));
1487 return false;
1488 }
1489 }
1490
1491 return true;
1492}
1493
1495 .name = "radius",
1496 .default_type_size = 1,
1497 .default_type_length = 1,
1498 .attr = {
1499 .flags = {
1500 .table = radius_flags,
1501 .table_len = NUM_ELEMENTS(radius_flags),
1502 .len = sizeof(fr_radius_attr_flags_t),
1503 },
1504 .valid = attr_valid,
1505 },
1506
1507 .init = fr_radius_global_init,
1508 .free = fr_radius_global_free,
1509
1510 .decode = fr_radius_decode_foreign,
1511 .encode = fr_radius_encode_foreign,
1512};
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:603
#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 UNUSED
Definition build.h:415
#define NUM_ELEMENTS(_t)
Definition build.h:437
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:4532
#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
int af
Address family.
Definition inet.h:64
union fr_ipaddr_t::@160 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: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
#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:1494
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:250
fr_dict_attr_t const * attr_nas_filter_rule
Definition base.c:62
static fr_dict_flag_parser_t const radius_flags[]
Definition base.c:221
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:175
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:1183
size_t fr_radius_limit_proxy_state_table_len
Definition base.c:102
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:361
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:839
fr_dict_attr_autoload_t libfreeradius_radius_dict_attr[]
Definition base.c:65
char const * fr_radius_decode_fail_reason[FR_RADIUS_FAIL_MAX+1]
Definition base.c:507
size_t fr_radius_require_ma_table_len
Definition base.c:93
fr_dict_attr_t const * attr_chap_challenge
Definition base.c:56
void * fr_radius_next_encodable(fr_dcursor_t *cursor, void *current, void *uctx)
Definition base.c:1016
fr_dict_attr_t const * attr_vendor_specific
Definition base.c:61
int fr_radius_global_init(void)
Definition base.c:1318
size_t fr_radius_request_name_table_len
Definition base.c:113
int fr_radius_allow_reply(fr_radius_packet_code_t code, bool allowed[static FR_RADIUS_CODE_MAX])
Definition base.c:230
#define FR_DEBUG_STRERROR_PRINTF
Definition base.c:83
fr_dict_attr_t const * attr_chargeable_user_identity
Definition base.c:57
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:1296
void fr_radius_global_free(void)
Definition base.c:1342
fr_table_num_sorted_t const fr_radius_limit_proxy_state_table[]
Definition base.c:95
fr_table_num_sorted_t const fr_radius_request_name_table[]
Definition base.c:104
fr_table_num_sorted_t const fr_radius_require_ma_table[]
Definition base.c:86
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:1033
#define FR_DEBUG_STRERROR_PRINTF_PUSH
Definition base.c:84
char const * fr_radius_packet_name[FR_RADIUS_CODE_MAX]
Definition base.c:115
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:288
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:2098
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:1978
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:1551
ssize_t fr_radius_encode_foreign(fr_dbuff_t *dbuff, fr_pair_list_t const *list)
Definition encode.c:1724
VQP attributes.
static char * secret
unsigned int has_tag
Attribute has a tag.
Definition radius.h:192
uint8_t request_code
original code for the request.
Definition radius.h:168
uint8_t const * request_authenticator
Definition radius.h:161
unsigned int abinary
Attribute is in "abinary" format.
Definition radius.h:193
fr_radius_attr_flags_encrypt_t
Definition radius.h:180
@ RADIUS_FLAG_ENCRYPT_INVALID
Invalid encryption flag.
Definition radius.h:181
@ RADIUS_FLAG_ENCRYPT_USER_PASSWORD
Encrypt attribute RFC 2865 style.
Definition radius.h:183
@ RADIUS_FLAG_ENCRYPT_ASCEND_SECRET
Encrypt attribute ascend style.
Definition radius.h:185
@ RADIUS_FLAG_ENCRYPT_TUNNEL_PASSWORD
Encrypt attribute RFC 2868 style.
Definition radius.h:184
char const * secret
Definition radius.h:127
unsigned int concat
Attribute is concatenated.
Definition radius.h:191
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:173
unsigned int extended
Attribute is an extended attribute.
Definition radius.h:190
fr_radius_attr_flags_encrypt_t encrypt
Attribute is encrypted.
Definition radius.h:194
bool require_message_authenticator
Definition radius.h:172
size_t secret_length
Definition radius.h:128
bool verify
can skip verify for dynamic clients
Definition radius.h:171
fr_radius_ctx_t const * common
Definition radius.h:159
@ 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:189
@ 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:166
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:208
TALLOC_CTX * tmp_ctx
for temporary things cleaned up during decoding
Definition radius.h:163
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