netpkt/dnssec.rs#
Source: lib/netpkt/dnssec.rs
dnssec -- DNSSEC's checks without I/O (RFC 4033-4035, 5155, 6840, 8624, 9276, 9824, 9905):
records and names in canonical form, signatures over RRsets, DS digests, and the proofs of
denial with NSEC and NSEC3. netio::dns asks for the records and builds the chain of trust.
records(&msg) -> Signed { message, answers, authority, additional }: each Record with its
owner and RDATA in canonical form (RFC 4034 6.2 and RFC 6840 5.1), taken from the bytes
as they came: a record written again from its parsed form need not be the same bytes
rrset(section, owner, type) / sigs(section, owner, type): an RRset and its RRSIGs
verify_rrset(set, sigs, keys, now, &Policy, &mut budget) -> Checked { ttl, signer, wildcard }
signing_input(set, &Rrsig): the bytes an RRSIG signs (for a signer as for the check)
verify_sig(alg, key, data, sig, &Policy) ds_matches(&Ds, &dnskey record) key_usable
deny_nsec / deny_nsec3(qname, qtype, ..) -> Denial (NXDOMAIN, NODATA, an opt-out delegation)
deny_ds(name, ..) -> DsDenial: an unsigned delegation, no delegation there, opt-out
wildcard_nsec / wildcard_nsec3: that the name a wildcard answered does not exist
nsec3_hash(name, salt, iterations) canonical_cmp(a, b) (RFC 4034 6.1) wire(name)
unbase32hex(label): an NSEC3 owner's hash
Algorithms: RSA/SHA-256 (8), RSA/SHA-512 (10), ECDSA P-256 / SHA-256 (13), P-384 / SHA-384
(14), Ed25519 (15), Ed448 (16). RSA/SHA-1 (5, and 7 for NSEC3) only with Policy::sha1: RFC
9905 has operators treat them as unsupported, which makes a zone signed only with them
insecure. DS digests: SHA-1 (1; RFC 9905 keeps it RECOMMENDED for validation), SHA-256 (2),
SHA-384 (4). Names here are wire bytes in lower case: labels with their lengths, the root's
zero at the end.
Limits against work an attacker can cause (KeyTrap, CVE-2023-50387; NSEC3 hashing,
CVE-2023-50868): verify_rrset counts every signature it computes against the caller's
budget; NSEC3 with more than NSEC3_MAX_ITERATIONS iterations gives Denial::Iterations (the
answer is insecure, RFC 9276 3.2) without hashing; at most NSEC3_MAX_RECORDS are looked at.
/// DNSSEC algorithm numbers (IANA's registry): RSA/SHA-1 (RFC 3110; unsupported by default, RFC 9905)
ALG_RSASHA1: u8 = 5
/// RSA/SHA-1 for NSEC3 zones (RFC 5155; unsupported by default)
ALG_RSASHA1_NSEC3_SHA1: u8 = 7
/// RSA/SHA-256 (RFC 5702)
ALG_RSASHA256: u8 = 8
/// RSA/SHA-512 (RFC 5702)
ALG_RSASHA512: u8 = 10
/// ECDSA on P-256 with SHA-256 (RFC 6605)
ALG_ECDSAP256SHA256: u8 = 13
/// ECDSA on P-384 with SHA-384 (RFC 6605)
ALG_ECDSAP384SHA384: u8 = 14
/// Ed25519 (RFC 8080)
ALG_ED25519: u8 = 15
/// Ed448 (RFC 8080)
ALG_ED448: u8 = 16
/// DS digest types: SHA-1 (RFC 4034; still checked, RFC 9905)
DIGEST_SHA1: u8 = 1
/// SHA-256 (RFC 4509)
DIGEST_SHA256: u8 = 2
/// SHA-384 (RFC 6605)
DIGEST_SHA384: u8 = 4
/// RFC 9824's NXNAME: in an NSEC's types, the name does not exist (a compact answer)
TYPE_NXNAME: u16 = 128
/// DNSKEY flag: a zone key (RFC 4034 2.1.1); only zone keys verify signatures
FLAG_ZONE: u16 = 0x0100
/// DNSKEY flag: revoked (RFC 5011 3); such a key verifies nothing here
FLAG_REVOKE: u16 = 0x0080
/// DNSKEY flag: a secure entry point, a KSK (RFC 4034 2.1.1; a hint only)
FLAG_SEP: u16 = 0x0001
/// above this NSEC3 counts as insecure (RFC 9276 3.2; BIND's limit)
NSEC3_MAX_ITERATIONS: u16 = 50
/// NSEC3 records looked at in one answer
NSEC3_MAX_RECORDS: usize = 32
struct Policywhat a validator acceptssha1: bool-- RSA/SHA-1 (algorithms 5 and 7) counts as supported; RFC 9905 has operators treat it as unsupported (false: a zone signed only with it is insecure)
impl Policy
const fn new() -> PolicyRFC 9905's: RSA/SHA-1 unsupportedfn alg_supported(alg: u8, p: &Policy) -> boolwhether signatures of this algorithm are checkedfn digest_supported(d: u8) -> boolwhether DS records of this digest type are checkedfn wire(name: &str) -> Option<Vec<u8>>a name in netpkt::dns's text form as wire bytes in lower case (None: not a valid name)fn text(n: &[u8]) -> Stringa wire name as text (netpkt::dns's form: the root as ".", no final dot otherwise)fn label_count(n: &[u8]) -> usizethe labels of a name as an RRSIG counts them: without the root and a leading "*"fn parent_of(n: &[u8]) -> &[u8]the name without its leftmost label (the root's parent is the root)fn is_below_or_eq(n: &[u8], zone: &[u8]) -> boolwhether n is zone or below it (wire names in lower case)fn canonical_cmp(a: &[u8], b: &[u8]) -> Orderingthe canonical order of names (RFC 4034 6.1): label by label from the right, each as bytes in lower case, a shorter one firststruct Recorda record as DNSSEC sees itname: String-- the owner as netpkt::dns writes itowner: Vec<u8>-- the owner in wire form, lower casertype: u16class: u16ttl: u32rdata: Vec<u8>-- the RDATA in canonical form: names expanded, in lower case where RFC 4034 6.2 (with RFC 6840 5.1: not in NSEC, not in HINFO) saysdata: RData
struct Signeda message with its records in canonical formmessage: Messageanswers: Vec<Record>authority: Vec<Record>additional: Vec<Record>
fn records(msg: &[u8]) -> Result<Signed, DnsErr>a message's records in canonical form, beside netpkt::dns's parse of itfn rrset<'a>(rs: &'a [Record], owner: &[u8], rtype: u16) -> Vec<&'a Record>the records of one RRset in a section (owner in wire form)fn sigs<'a>(rs: &'a [Record], owner: &[u8], rtype: u16) -> Vec<(&'a Record, &'a Rrsig)>the RRSIGs in a section that cover an RRsetenum Failwhy an RRset or a denial does not countNoKey-- no signature of a supported algorithm by one of the keys givenSignature-- signatures by the keys, none of them validTime-- the signatures are outside their timeMalformed-- an RRSIG that does not fit its RRset (labels, signer, owner, class)Budget-- more signatures than the budget allows (colliding key tags: KeyTrap)Proof-- a denial without the proof it needs
struct Checkedwhat a valid signature tellsttl: u32-- how long the RRset may be kept: its records' TTL, the RRSIG's original TTL and the time left of the signature, the lowest (RFC 4035 5.3.3)signer: Vec<u8>-- the zone that signed (wire)wildcard: Option<Vec<u8>>-- the owner was expanded from a wildcard: the wildcard's parent, the closest encloser (the caller must see the proof that the name itself does not exist)
fn key_usable(key: &Record) -> boolwhether a DNSKEY record may verify signatures: a zone key of protocol 3, not revokedfn signing_input(set: &[&Record], s: &Rrsig) -> Option<Vec<u8>>the bytes an RRSIG over an RRset signs (RFC 4034 3.1.8.1): the RRSIG's RDATA without the signature (its signer in lower case), then the RRset's records in canonical form and order, each once, with the original TTL; a wildcard's owner as "*" and the RRSIG's labels (an answer expanded from it). What a signer signs and verify_rrset checks; None when the signer is no name or the labels are more than the owner hasfn verify_rrset(set: &[&Record], sigs: &[(&Record, &Rrsig)], keys: &[&Record], now: u32, p: &Policy, budget: &mut u32) -> Result<Checked, Fail>an RRset checked against its RRSIGs and the keys of the zone that signed it (validated DNSKEY records): Ok with the first signature that verifies. Each signature computed takes one from budgetfn verify_sig(alg: u8, key: &[u8], data: &[u8], sig: &[u8], p: &Policy) -> boolone signature of a DNSSEC algorithm over data with a DNSKEY's public keyfn ds_digest(owner: &[u8], dnskey_rdata: &[u8], digest_type: u8) -> Option<Vec<u8>>the digest a DS of this type holds for a DNSKEY (owner in wire form, the key's RDATA)fn ds_matches(ds: &Ds, key: &Record) -> boolwhether a DS names this DNSKEY record (tag, algorithm and digest; a digest type checked)enum Denialwhat a proof of denial showsNoName-- the name does not exist (NXDOMAIN; RFC 9824's compact answer: NXNAME)NoData-- the name exists without records of the type (NODATA; an empty non-terminal too)OptOut-- NSEC3 opt-out covers the name: an unsigned delegation may be there, so what the answer says is insecure (RFC 5155 6, 8.6, 9.2)Iterations-- NSEC3 with more iterations than NSEC3_MAX_ITERATIONS: insecure (RFC 9276 3.2)
fn deny_nsec(qname: &[u8], qtype: u16, rs: &[&Record], zone: &[u8]) -> Result<Denial, Fail>what validated NSEC records (owners and signatures checked by the caller, from the zone whose apex is zone) prove about qname and qtypeenum DsDenialwhat a DS query's NODATA answer (from the parent zone) proves about the name nDelegation-- n is a delegation without DS: the zone below is unsignedNoCut-- n is a name inside the zone (or an empty non-terminal): no delegation thereOptOut-- NSEC3 opt-out covers n: an unsigned delegation may be there (RFC 5155 6)Iterations-- NSEC3 with more iterations than NSEC3_MAX_ITERATIONS: insecure
fn deny_ds(n: &[u8], rs: &[&Record], zone: &[u8]) -> Result<DsDenial, Fail>the proof of validated NSEC or NSEC3 records of the zone (its apex: zone) that n has no DSfn wildcard_nsec(qname: &[u8], ce: &[u8], rs: &[&Record]) -> Result<(), Fail>that the name a wildcard answered for does not exist, nor any name between it and the closest encloser ce (from the RRSIG's labels, Checked::wildcard; RFC 4035 5.3.4): an NSEC covers the next closer name (one label below ce on the way to qname), and its next name is not below it (that would make the next closer name an empty non-terminal, which the wildcard does not answer for)fn unbase32hex(s: &[u8]) -> Option<Vec<u8>>base32hex without padding (RFC 4648 7), as an NSEC3 owner's first label holds the hashfn nsec3_hash(name: &[u8], salt: &[u8], iterations: u16) -> [u8; 20]NSEC3's hash of a name (RFC 5155 5): SHA-1 over the name and the salt, iterations times morefn deny_nsec3(qname: &[u8], qtype: u16, rs: &[&Record], zone: &[u8]) -> Result<Denial, Fail>what validated NSEC3 records of the zone whose apex is zone prove about qname and qtypefn wildcard_nsec3(qname: &[u8], ce: &[u8], rs: &[&Record], zone: &[u8]) -> Result<Option<Denial>, Fail>that the name a wildcard answered for does not exist: the next closer name below the closest encloser is covered (RFC 5155 8.8); Denial::OptOut when an opt-out NSEC3 covers it