tools
encode_string(origstr)
Encode a string to bytes, ensuring it is UTF-8 encoded.
Source code in pyrad2/tools.py
encode_octets(octetstring)
Encode raw octet string (already in bytes).
Length-capping is the AVP layer's job — fragmenting attributes
(concat, RFC 6929 long-extended, RFC 5904 WiMAX
continuation) legitimately encode logical values larger than one
AVP's 253-byte payload field. This function only ensures the
string-form 0x... hex input doesn't expand past what the AVP
layer can handle in one shot.
Source code in pyrad2/tools.py
encode_address(addr)
Encode an IPv4 address (dotted string) to 4-byte format.
encode_ipv6_prefix(addr, default_prefixlen=128)
Encode an IPv6 address and prefix length to 18-byte format.
Source code in pyrad2/tools.py
encode_ipv4_prefix(addr, default_prefixlen=32)
Encode an IPv4 prefix (RFC 5090) to 6-byte format.
The wire layout is: 1 reserved byte (0), 1 prefix-length byte
(0..32), 4 bytes of network address. Bits beyond the prefix
length are zeroed. Accepts "a.b.c.d/len", a bare "a.b.c.d"
(treated as /default_prefixlen), an IPv4Network, or an
IPv4Address.
Source code in pyrad2/tools.py
encode_ipv6_address(addr)
Encode an IPv6 address (as string) to 16-byte format.
Source code in pyrad2/tools.py
encode_combo_ip(addr)
Encode an IPv4 or IPv6 address for a combo-ip attribute.
FreeRADIUS's combo-ip type carries either an IPv4 (4 bytes) or
an IPv6 (16 bytes) address — the wire length tells which. The
address family is decided here by inspecting the input: a string is
parsed by ip_address, which returns the right family natively.
Source code in pyrad2/tools.py
encode_ifid(value)
Encode an 8-byte Interface-Id (RFC 3162) from xxxx:xxxx:xxxx:xxxx form.
Bytes already of length 8 are passed through unchanged so that dictionary VALUE entries — which arrive pre-encoded — round-trip cleanly.
Source code in pyrad2/tools.py
decode_ifid(value)
Decode 8-byte Interface-Id (RFC 3162) into xxxx:xxxx:xxxx:xxxx form.
Source code in pyrad2/tools.py
encode_ether(value)
Encode a 6-byte Ethernet MAC address from hh:hh:hh:hh:hh:hh form.
Accepts both colon and hyphen separators. Bytes of length 6 pass through.
Source code in pyrad2/tools.py
decode_ether(value)
Decode a 6-byte Ethernet MAC address into hh:hh:hh:hh:hh:hh form.
encode_ascend_binary(orig_str)
Encode binary data in Ascend-specific format (length prefixed).
Source code in pyrad2/tools.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 | |
encode_integer(num, format='!I')
Encode a 32-bit unsigned integer to 4-byte big-endian.
Source code in pyrad2/tools.py
encode_integer64(num, format='!Q')
Encode a 64-bit unsigned integer to 8-byte big-endian.
Source code in pyrad2/tools.py
encode_date(num)
Encode a UNIX timestamp (int) to 4-byte format.
decode_string(orig_str)
Decode UTF-8 bytes into a string.
decode_octets(orig_bytes)
decode_address(addr)
decode_ipv6_prefix(addr)
Decode 18-byte IPv6 prefix format into address/prefix tuple.
Source code in pyrad2/tools.py
decode_ipv4_prefix(addr)
Decode an IPv4 prefix (RFC 5090) into a.b.c.d/len form.
Wire layout: 1 reserved byte, 1 prefix-length byte, then 0..4 bytes
of address (the variable form FreeRADIUS emits when bits beyond the
prefix length are trimmed). Short payloads are zero-padded to a full
4-byte address; strict=False masks any nonzero host bits a peer
happens to leave on the wire.
Source code in pyrad2/tools.py
decode_ipv6_address(addr)
Decode 16-byte IPv6 address into a readable string.
Source code in pyrad2/tools.py
decode_combo_ip(addr)
Decode a combo-ip attribute, dispatching on wire length.
4 bytes is an IPv4 address; 16 bytes an IPv6 address. Any other length is invalid — combo-ip has no other valid encoding.
Source code in pyrad2/tools.py
decode_ascend_binary(orig_bytes)
decode_integer(num, format='!I')
decode_integer64(num, format='!Q')
decode_date(num)
encode_attr(datatype, value)
Encode a RADIUS attribute (type, value, length) into bytes.
Source code in pyrad2/tools.py
decode_attr(datatype, value)
Decode a RADIUS attribute from bytes into a type and value.
Source code in pyrad2/tools.py
get_cert_fingerprint(cert)
Generate SHA-256 fingerprint from a certificate.
Source code in pyrad2/tools.py
normalize_cert_fingerprint(fingerprint)
Normalize a SHA-256 certificate fingerprint for comparison.
Accepts plain hex, colon-separated hex, and values prefixed with
sha256:. Raises ValueError when the normalized value is not a 64
character hexadecimal SHA-256 fingerprint.
Source code in pyrad2/tools.py
cert_fingerprint_matches(cert, allowed_fingerprints)
Return True when a DER certificate's SHA-256 fingerprint is allowed.
read_radius_packet(reader)
async
Read a full RADIUS packet from the stream.
There's no built-in framing in RadSec, so we can't read a fixed-size packet. Instead, we read the header first to determine the length of the packet, and then read the rest of the packet based on that length.
RADIUS packets are prefixed with a 4-byte header
- Code (1 byte)
- Identifier (1 byte)
- Length (2 bytes)
The length includes the header, so the minimum length is 20 bytes (4-byte header + 16-byte Authenticator). If the length is less than 20, it is considered invalid.
:param reader: asyncio StreamReader to read from :return: Full RADIUS packet as bytes