JFLib

netpkt/radius.rs#

Source: lib/netpkt/radius.rs

radius -- RADIUS packets: authentication (RFC 2865), accounting (RFC 2866) and the
Message-Authenticator (RFC 3579 3.2): codes, attributes by number and name, the hiding of
User-Password (RFC 2865 5.2) both ways, the Request and Response Authenticators.  No digest
is computed here (netpkt has no cryptography): the caller lends MD5 and HMAC-MD5, as
crypto::md5_parts and crypto::hmac_md5 are

  PORT_AUTH 1812, PORT_ACCT 1813, PORT_AUTH_OLD 1645, PORT_ACCT_OLD 1646 (old servers);
  is_port(p)
  ACCESS_REQUEST 1, ACCESS_ACCEPT 2, ACCESS_REJECT 3, ACCOUNTING_REQUEST 4,
  ACCOUNTING_RESPONSE 5, ACCESS_CHALLENGE 11, STATUS_SERVER 12, STATUS_CLIENT 13; code_name
  A_* the attributes of RFC 2865 (1 .. 39, 60 .. 63), RFC 2866 (40 .. 51) and RFC 3579 (79
  EAP-Message, 80 Message-Authenticator); attr_name, attr_kind (Kind), value_name (the named
  values of Service-Type, Framed-Protocol, Login-Service, NAS-Port-Type, Acct-Status-Type,
  Acct-Authentic, Acct-Terminate-Cause, Termination-Action) and the values themselves
  (SERVICE_*, PORT_TYPE_*, ACCT_*, TERM_*)
  RadiusView::parse(b)            code, identifier, length, authenticator and the attributes
                                  with their offsets (attrs()); Bad says why bytes are none
  Packet { code, id, auth, attrs }    read(view) / parse(b) / to_bytes; attr / all / text /
                                  int / addr of an attribute type
  Attr { typ, value }             Attr::text / int / addr / bytes, Attr::vsa(vendor, data) and
                                  vendor() (Vendor-Specific: the vendor's number and its data
                                  kept as bytes)
  hide_password(pw, secret, auth, md5) / reveal_password(hidden, secret, auth, md5)
  response_auth(b, req_auth, secret, md5)   the Response Authenticator of a written answer;
                                  set_response_auth / check_response_auth
  accounting_auth(b, secret, md5) the Request Authenticator of an Accounting-Request
                                  (RFC 2866 3); set_accounting_auth / check_accounting_auth
  message_auth(b, auth, secret, hmac)   the Message-Authenticator over a packet (its field
                                  counted as zero, `auth` in the authenticator field: the
                                  packet's own for a request, the request's for an answer);
                                  set_message_auth / check_message_auth (None: there is none)
  Packet::push_eap(eap) / eap()   an EAP packet in EAP-Message attributes of 253 bytes and back
                                  (RFC 3579 3.1); a packet with one needs a Message-Authenticator
  Packet::push_vlan(vlan) / vlan()   VLAN assignment: Tunnel-Type VLAN, Tunnel-Medium-Type 802,
                                  Tunnel-Private-Group-ID (RFC 2868, 3580 3.31)
  mppe_seal(key, secret, auth, salt, md5) / mppe_open(value, secret, auth, md5)   the keys of
                                  MS-MPPE-Send-Key and -Recv-Key (RFC 2548 2.4.2): salted and
                                  hidden with the shared secret and the Request Authenticator
  Packet::push_msk(msk, secret, auth, salts, md5) / msk(secret, auth, md5)   an EAP method's MSK
                                  in both of them (Recv first: RFC 3079, hostapd's order)

RFC 2865 3: a packet is 20 to 4096 bytes; octets beyond its Length field are padding, a
packet shorter than its Length is discarded, and so is one whose attributes do not end
exactly at Length.  Attribute values are at most 253 bytes.  A Message-Authenticator is
added as 16 zero bytes (Attr::message_auth()) and filled in last but before the Response
Authenticator, which covers it.
  • type Md5Fn = fn(&[&[u8]]) -> [u8; 16] MD5 of the concatenation of the parts (what the caller lends: crypto::md5_parts)

  • type HmacMd5Fn = fn(&[u8], &[u8]) -> [u8; 16] HMAC-MD5 with a key over data (crypto::hmac_md5)

PORT_AUTH: u16 = 1812
PORT_ACCT: u16 = 1813
/// the ports servers used before RFC 2865 (Cisco IOS still sends from them)
PORT_AUTH_OLD: u16 = 1645
PORT_ACCT_OLD: u16 = 1646
  • fn is_port(p: u16) -> bool a port RADIUS uses
ACCESS_REQUEST: u8 = 1
ACCESS_ACCEPT: u8 = 2
ACCESS_REJECT: u8 = 3
ACCOUNTING_REQUEST: u8 = 4
ACCOUNTING_RESPONSE: u8 = 5
ACCESS_CHALLENGE: u8 = 11
/// experimental (RFC 2865 3, RFC 5997)
STATUS_SERVER: u8 = 12
STATUS_CLIENT: u8 = 13
/// code, identifier, length, authenticator
HDR: usize = 20
/// the largest packet (RFC 2865 3)
MAX: usize = 4096
/// the longest attribute value
VALUE_MAX: usize = 253
  • fn code_name(c: u8) -> &'static str the name of a code
// the attributes of RFC 2865 5
A_USER_NAME: u8 = 1
A_USER_PASSWORD: u8 = 2
A_CHAP_PASSWORD: u8 = 3
A_NAS_IP_ADDRESS: u8 = 4
A_NAS_PORT: u8 = 5
A_SERVICE_TYPE: u8 = 6
A_FRAMED_PROTOCOL: u8 = 7
A_FRAMED_IP_ADDRESS: u8 = 8
A_FRAMED_IP_NETMASK: u8 = 9
A_FRAMED_ROUTING: u8 = 10
A_FILTER_ID: u8 = 11
A_FRAMED_MTU: u8 = 12
A_FRAMED_COMPRESSION: u8 = 13
A_LOGIN_IP_HOST: u8 = 14
A_LOGIN_SERVICE: u8 = 15
A_LOGIN_TCP_PORT: u8 = 16
A_REPLY_MESSAGE: u8 = 18
A_CALLBACK_NUMBER: u8 = 19
A_CALLBACK_ID: u8 = 20
A_FRAMED_ROUTE: u8 = 22
A_FRAMED_IPX_NETWORK: u8 = 23
A_STATE: u8 = 24
A_CLASS: u8 = 25
A_VENDOR_SPECIFIC: u8 = 26
A_SESSION_TIMEOUT: u8 = 27
A_IDLE_TIMEOUT: u8 = 28
A_TERMINATION_ACTION: u8 = 29
A_CALLED_STATION_ID: u8 = 30
A_CALLING_STATION_ID: u8 = 31
A_NAS_IDENTIFIER: u8 = 32
A_PROXY_STATE: u8 = 33
A_LOGIN_LAT_SERVICE: u8 = 34
A_LOGIN_LAT_NODE: u8 = 35
A_LOGIN_LAT_GROUP: u8 = 36
A_FRAMED_APPLETALK_LINK: u8 = 37
A_FRAMED_APPLETALK_NETWORK: u8 = 38
A_FRAMED_APPLETALK_ZONE: u8 = 39
// RFC 2866 5
A_ACCT_STATUS_TYPE: u8 = 40
A_ACCT_DELAY_TIME: u8 = 41
A_ACCT_INPUT_OCTETS: u8 = 42
A_ACCT_OUTPUT_OCTETS: u8 = 43
A_ACCT_SESSION_ID: u8 = 44
A_ACCT_AUTHENTIC: u8 = 45
A_ACCT_SESSION_TIME: u8 = 46
A_ACCT_INPUT_PACKETS: u8 = 47
A_ACCT_OUTPUT_PACKETS: u8 = 48
A_ACCT_TERMINATE_CAUSE: u8 = 49
A_ACCT_MULTI_SESSION_ID: u8 = 50
A_ACCT_LINK_COUNT: u8 = 51
// RFC 2865 5 again
A_CHAP_CHALLENGE: u8 = 60
A_NAS_PORT_TYPE: u8 = 61
A_PORT_LIMIT: u8 = 62
A_LOGIN_LAT_PORT: u8 = 63
// RFC 3579
A_EAP_MESSAGE: u8 = 79
A_MESSAGE_AUTHENTICATOR: u8 = 80
// RFC 2868: tunnels, which also assign VLANs (RFC 3580 3.31)
A_TUNNEL_TYPE: u8 = 64
A_TUNNEL_MEDIUM_TYPE: u8 = 65
A_TUNNEL_PRIVATE_GROUP_ID: u8 = 81
/// Tunnel-Type VLAN, Tunnel-Medium-Type IEEE-802
TUNNEL_VLAN: u32 = 13
TUNNEL_MEDIUM_802: u32 = 6
/// the vendor of MS-MPPE-Send-Key and MS-MPPE-Recv-Key (RFC 2548)
VENDOR_MICROSOFT: u32 = 311
MS_MPPE_SEND_KEY: u8 = 16
MS_MPPE_RECV_KEY: u8 = 17
/// Service-Type (RFC 2865 5.6)
SERVICE_LOGIN: u32 = 1
SERVICE_FRAMED: u32 = 2
SERVICE_CALLBACK_LOGIN: u32 = 3
SERVICE_CALLBACK_FRAMED: u32 = 4
SERVICE_OUTBOUND: u32 = 5
SERVICE_ADMINISTRATIVE: u32 = 6
SERVICE_NAS_PROMPT: u32 = 7
SERVICE_AUTHENTICATE_ONLY: u32 = 8
/// NAS-Port-Type (RFC 2865 5.41)
PORT_TYPE_ASYNC: u32 = 0
PORT_TYPE_VIRTUAL: u32 = 5
PORT_TYPE_ETHERNET: u32 = 15
PORT_TYPE_WIRELESS: u32 = 19
/// Acct-Status-Type (RFC 2866 5.1)
ACCT_START: u32 = 1
ACCT_STOP: u32 = 2
ACCT_INTERIM: u32 = 3
ACCT_ON: u32 = 7
ACCT_OFF: u32 = 8
/// Acct-Authentic (RFC 2866 5.6)
AUTHENTIC_RADIUS: u32 = 1
AUTHENTIC_LOCAL: u32 = 2
AUTHENTIC_REMOTE: u32 = 3
/// Acct-Terminate-Cause (RFC 2866 5.10)
TERM_USER_REQUEST: u32 = 1
TERM_LOST_CARRIER: u32 = 2
TERM_LOST_SERVICE: u32 = 3
TERM_IDLE_TIMEOUT: u32 = 4
TERM_SESSION_TIMEOUT: u32 = 5
TERM_ADMIN_RESET: u32 = 6
TERM_ADMIN_REBOOT: u32 = 7
TERM_NAS_ERROR: u32 = 9
TERM_NAS_REQUEST: u32 = 10
TERM_NAS_REBOOT: u32 = 11
  • fn attr_name(t: u8) -> String the name of an attribute type

  • enum Kind what the value of an attribute is (RFC 2865 5: text, string, address, integer, time)

    • Text -- UTF-8 text
    • String -- bytes
    • Address -- an IPv4 address (4 bytes)
    • Integer -- a 32-bit number (some with named values)
    • Time -- seconds since 1970 (32 bits)
    • Hidden -- User-Password: hidden with the shared secret
    • Vendor -- a vendor's number and its data
    • Unknown -- unknown types: bytes
  • fn attr_kind(t: u8) -> Kind the kind of an attribute's value

  • fn value_name(t: u8, v: u32) -> Option<&'static str> the name of a value of an integer attribute that has named values

  • enum Bad why bytes are no RADIUS packet (a receiver discards them silently)

    • Short -- shorter than the header, or than the Length field says
    • Length(u16) -- a Length field below 20 or above 4096
    • Attr(usize) -- an attribute with a length below 2, or one that does not end inside the packet
  • struct Attr an attribute

    • typ: u8
    • value: Vec<u8>

impl Attr

  • fn bytes(typ: u8, value: &[u8]) -> Attr

  • fn text(typ: u8, s: &str) -> Attr

  • fn int(typ: u8, v: u32) -> Attr

  • fn addr(typ: u8, a: Ipv4) -> Attr

  • fn vsa(vendor: u32, data: &[u8]) -> Attr a Vendor-Specific attribute: the vendor's SMI number and its data, kept as bytes

  • fn message_auth() -> Attr a Message-Authenticator to be filled in (set_message_auth)

  • fn vendor(&self) -> Option<(u32, &[u8])> the vendor and its data of a Vendor-Specific attribute

  • fn as_int(&self) -> Option<u32> the value as a number (4 bytes)

  • fn as_addr(&self) -> Option<Ipv4> the value as an address (4 bytes)

  • fn as_text(&self) -> String the value as text (bytes that are no UTF-8 are replaced)

  • struct RadiusView<'a> a RADIUS packet, checked once

impl<'a> RadiusView<'a>

  • fn parse(b: &'a [u8]) -> Result<RadiusView<'a>, Bad> the packet at the start of b (bytes after its Length are padding)

  • fn code(&self) -> u8

  • fn id(&self) -> u8

  • fn length(&self) -> u16

  • fn authenticator(&self) -> [u8; 16]

  • fn bytes(&self) -> &'a [u8] the packet's bytes (up to Length)

  • fn attrs(&self) -> Vec<(usize, u8, &'a [u8])> every attribute: its offset in the packet, its type and its value

  • struct Packet a RADIUS packet as a record

    • code: u8
    • id: u8
    • auth: [u8; 16] -- the Request or Response Authenticator
    • attrs: Vec<Attr>

impl Packet

  • fn new(code: u8, id: u8, auth: [u8; 16]) -> Packet

  • fn read(v: &RadiusView) -> Packet

  • fn parse(b: &[u8]) -> Result<Packet, Bad>

  • fn to_bytes(&self) -> Vec<u8> the bytes (an attribute value longer than 253 bytes is cut; the whole is at most 4096)

  • fn push(&mut self, a: Attr) -> &mut Packet

  • fn attr(&self, t: u8) -> Option<&Attr> the first attribute of a type

  • fn all(&self, t: u8) -> Vec<&Attr> every attribute of a type

  • fn text(&self, t: u8) -> Option<String>

  • fn int(&self, t: u8) -> Option<u32>

  • fn addr(&self, t: u8) -> Option<Ipv4>

  • fn hide_password(pw: &[u8], secret: &[u8], auth: &[u8; 16], md5: Md5Fn) -> Vec<u8> User-Password hidden: padded with zeros to a multiple of 16 (at most 128 bytes), each block XORed with MD5(secret + the previous block of the result; the Request Authenticator first)

  • fn reveal_password(hidden: &[u8], secret: &[u8], auth: &[u8; 16], md5: Md5Fn) -> Option<Vec<u8>> the password of a hidden User-Password (its padding zeros removed); None when the length is no multiple of 16 between 16 and 128

  • fn response_auth(b: &[u8], req_auth: &[u8; 16], secret: &[u8], md5: Md5Fn) -> [u8; 16] the Response Authenticator of a written answer to a request with req_auth

  • fn set_response_auth(b: &mut [u8], req_auth: &[u8; 16], secret: &[u8], md5: Md5Fn) writes the Response Authenticator into an answer (after a Message-Authenticator)

  • fn check_response_auth(b: &[u8], req_auth: &[u8; 16], secret: &[u8], md5: Md5Fn) -> bool the answer's Response Authenticator is right for the request

  • fn accounting_auth(b: &[u8], secret: &[u8], md5: Md5Fn) -> [u8; 16] the Request Authenticator of an Accounting-Request (RFC 2866 3: over 16 zero bytes)

  • fn set_accounting_auth(b: &mut [u8], secret: &[u8], md5: Md5Fn) writes the Request Authenticator into an Accounting-Request

  • fn check_accounting_auth(b: &[u8], secret: &[u8], md5: Md5Fn) -> bool an Accounting-Request's Request Authenticator fits the secret

  • fn message_auth(b: &[u8], auth: &[u8; 16], secret: &[u8], hmac: HmacMd5Fn) -> [u8; 16] HMAC-MD5 with the secret over the packet with auth in its authenticator field and the Message-Authenticator counted as 16 zero bytes (RFC 3579 3.2)

  • fn set_message_auth(b: &mut [u8], auth: Option<&[u8; 16]>, secret: &[u8], hmac: HmacMd5Fn) -> bool fills in the Message-Authenticator of a written packet (auth: None for a request, which covers its own authenticator; the request's for an answer); false when there is none

  • fn check_message_auth(b: &[u8], auth: Option<&[u8; 16]>, secret: &[u8], hmac: HmacMd5Fn) -> Option<bool> the Message-Authenticator of a packet fits the secret (None: the packet has none)

impl Packet

  • fn push_eap(&mut self, eap: &[u8]) -> &mut Packet an EAP packet in EAP-Message attributes (RFC 3579 3.1: pieces of at most 253 bytes)

  • fn eap(&self) -> Option<Vec<u8>> the EAP packet of the EAP-Message attributes; None without one

  • fn push_vlan(&mut self, vlan: u16) -> &mut Packet the attributes of a VLAN assignment (tag 0: no tunnel group)

  • fn vlan(&self) -> Option<u16> the VLAN these attributes assign: all three are there, Tunnel-Type VLAN, the medium 802, a number (a tag byte before the number is skipped); a name of a VLAN is no number here

  • fn push_msk(&mut self, msk: &[u8; 64], secret: &[u8], auth: &[u8; 16], salts: [u16; 2], md5: Md5Fn) -> &mut Packet an MSK of 64 bytes as MS-MPPE-Recv-Key (its first half) and MS-MPPE-Send-Key

  • fn msk(&self, secret: &[u8], auth: &[u8; 16], md5: Md5Fn) -> Option<[u8; 64]> the MSK out of both MS-MPPE attributes (None: one is missing or does not open)

  • fn mppe_seal(key: &[u8], secret: &[u8], auth: &[u8; 16], salt: u16, md5: Md5Fn) -> Vec<u8> the value of an MS-MPPE key attribute (RFC 2548 2.4.2): the salt (its high bit set), then a length byte, the key and padding, each 16 bytes XORed with MD5(secret + the previous block; the Request Authenticator and the salt first)

  • fn mppe_open(value: &[u8], secret: &[u8], auth: &[u8; 16], md5: Md5Fn) -> Option<Vec<u8>> the key of an MS-MPPE key attribute; None when its length byte does not fit

  • fn value_text(t: u8, v: &[u8]) -> String the value of an attribute as a dissector shows it (a hidden password stays hidden)

Source: docs/API.md in the JFLib repository.