dns_sec (dns_erlang v6.0.0)
View SourceThe dns_sec module exports functions used for generating NSEC/NSEC3 records,
signing and verifying RRSIGs, and adding keytags to DNSKEY records.
For example, the sign_rr/6 function can be given a collection of resource records,
the signer name, keytag, signing algorithm, private key, and a collection of options
and it will return a list of RRSIG records. Supported signing algorithms include
DSA, RSA (SHA1/SHA256/SHA512), ECDSA (P-256/P-384), Ed25519, Ed448, and ML-DSA-44.
ML-DSA-44 (algorithm 18) follows draft-westerbaan-dnssec-mldsa-04, which may still change.
It needs OTP 28.1+ built against OpenSSL 3.5+; elsewhere, signing raises
{unsupported_algorithm, 18} and verifying returns false. The private key is the 32-byte
seed or the 2560-byte expanded key. Signatures are hedged, so they differ from the draft's
deterministic example but verify the same.
Summary
Types
DNSKEY resource record data.
DS (Delegation Signer) resource record data.
Options for gen_nsec3/2.
Options for gen_nsec/4.
A private key to sign with.
DNSKEY key tag (RFC 4034 Appendix B).
NSEC3 resource record data.
NSEC3 hash algorithm identifier (currently only SHA-1).
Custom hash function for use with ih/4.
NSEC3 iteration count.
NSEC3 salt value.
NSEC resource record data.
RRSIG resource record data.
DNSSEC signing algorithm identifier.
Options for sign_rr/6 and sign_rrset/6.
Options for verify_rrsig/4.
Functions
Generates and appends a CDNSKEY records key tag.
Generates and appends a DNS Key records key tag.
Converts a resource record data record to DNSSEC canonical form.
Returns the public key field of a DNSKEY's RDATA for a public key.
Equivalent to gen_nsec3(RRs, #{}).
Generate NSEC3 records from a list of dns:rr/0.
Generate NSEC records from a list of dns:rr/0.
Equivalent to gen_nsec(ZoneName, RR, TTL, #{}).
Generate NSEC records.
NSEC3 iterative hash function.
Equivalent to sign_rr(RR, SignerName, KeyTag, Alg, Key, []).
Signs each RRset in a list of dns:rr/0, as sign_rrset/6 does.
Equivalent to sign_rrset(RRSet, SignerName, KeyTag, Alg, Key, []).
Signs a list of dns:rr/0 of the same class and type.
Verifies RRSet against the RRSIG record, with one of the DNSKEY records in DNSKeys,
at the time now in Opts, by default the current time.
Types
-type dnskey() :: #dns_rrdata_dnskey{flags :: dns:uint16(), protocol :: dns:uint8(), alg :: dns:uint8(), public_key :: binary(), keytag :: integer()}.
DNSKEY resource record data.
-type ds() :: #dns_rrdata_ds{keytag :: dns:uint16(), alg :: dns:uint8(), digest_type :: dns:uint8(), digest :: binary()}.
DS (Delegation Signer) resource record data.
-type gen_nsec3_opts() :: gen_nsec_opts().
Options for gen_nsec3/2.
-type gen_nsec_opts() :: #{base_types => [dns:type()]}.
Options for gen_nsec/4.
A private key to sign with.
An RSA key is [E, N, D], or the whole list crypto:generate_key/2 gives, which lets crypto sign
by the CRT, and a DSA key [P, Q, G, X], each number as its big-endian bytes; a number given as an
integer raises {badarg, number_not_bytes}. An ECDSA key is its private scalar, an EdDSA key its
private key, and an ML-DSA-44 key its 32-byte seed or 2560-byte expanded key. The public key a
DNSKEY carries is its RDATA's key field, which dnskey_public_key/2 makes.
-type keytag() :: integer().
DNSKEY key tag (RFC 4034 Appendix B).
-type nsec3() :: #dns_rrdata_nsec3{hash_alg :: dns:uint8(), opt_out :: boolean(), iterations :: dns:uint16(), salt :: binary(), hash :: binary(), types :: [non_neg_integer()]}.
NSEC3 resource record data.
-type nsec3_hashalg() :: 1.
NSEC3 hash algorithm identifier (currently only SHA-1).
Custom hash function for use with ih/4.
-type nsec3_iterations() :: non_neg_integer().
NSEC3 iteration count.
-type nsec3_salt() :: binary().
NSEC3 salt value.
-type nsec() :: #dns_rrdata_nsec{next_dname :: dns:dname(), types :: [non_neg_integer()]}.
NSEC resource record data.
-type rrsig() :: #dns_rrdata_rrsig{type_covered :: dns:uint16(), alg :: 1 | 3 | 5 | 6 | 7 | 8 | 10 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 23, labels :: dns:uint8(), original_ttl :: dns:uint32(), expiration :: dns:uint32(), inception :: dns:uint32(), keytag :: dns:uint16(), signers_name :: dns:dname(), signature :: binary()}.
RRSIG resource record data.
-type sigalg() :: 3 | 6 | 5 | 7 | 8 | 10 | 13 | 14 | 15 | 16 | 18.
DNSSEC signing algorithm identifier.
Unlike dns:alg/0, this type is restricted to algorithms valid for zone signing.
-type sign_rr_opts() :: #{inception => dns:unix_time(), expiration => dns:unix_time()}.
Options for sign_rr/6 and sign_rrset/6.
-type verify_rrsig_opts() :: #{now => dns:unix_time()}.
Options for verify_rrsig/4.
Functions
Generates and appends a CDNSKEY records key tag.
Generates and appends a DNS Key records key tag.
-spec canonical_rrdata_form(dns:rrdata()) -> dns:rrdata().
Converts a resource record data record to DNSSEC canonical form.
Returns the public key field of a DNSKEY's RDATA for a public key.
An RSA key [E, N] is laid out as RFC 3110 §2 has it, and a DSA key
[P, Q, G, Y] as RFC 2536 §2 has it, each number as its big-endian bytes, as
crypto:generate_key/2 gives an RSA key's. An ECDSA key, the point that
crypto:generate_key(ecdh, Curve) gives, loses the octet 4 in front that marks it
uncompressed (RFC 6605 §4), and an EdDSA or ML-DSA-44 key, as
crypto:generate_key/2 gives it, is as it is.
Raises {unsupported_algorithm, Alg} for an algorithm without a key layout here,
and {badarg, Why} for a key the algorithm's DNSKEY cannot carry. Why names
the rule the key breaks and, for RSA and DSA keys, the size in bits of the
number that breaks it, but never one of the key's numbers:
number_not_bytes: an RSA or DSA key's number given as anything but its bytes, such as an integer.{rsa_exponent_bits, Bits}: an RSA exponent that is empty or over 4096 bits long (RFC 3110 §2).{rsa_modulus_bits, Bits}: an RSA modulus that is not 512 to 4096 bits long, or 1024 to 4096 for RSA/SHA-512 (RFC 3110 §3, RFC 5702 §2).{dsa_q_bits, Bits}: a DSA Q that is not 160 bits long (RFC 2536 §2).{dsa_p_bits, Bits}: a DSA P that is not 512 + 64T bits long for a T of 0 to 8.dsa_g_out_of_range: a DSA G that is not 1 < G < P (FIPS 186-4 §4.1).dsa_y_out_of_range: a DSA Y that is not 2 ≤ Y ≤ P − 2, the range NIST SP 800-89 checks a public key against: a G and Y of 1 verify the signature R = S = 1 over any data.key_shape: a key of another shape than the algorithm's: another count of numbers, an ECDSA point without the octet 4 in front, or an EdDSA or ML-DSA-44 key of another length.
verify_rrsig/4 verifies with no RSA or DSA key that this function would not
make, and dns_json:from_map/1 and dns_zone refuse a DNSKEY or CDNSKEY with
one.
Equivalent to gen_nsec3(RRs, #{}).
-spec gen_nsec3([dns:rr()], gen_nsec3_opts()) -> [dns:rr()].
Generate NSEC3 records from a list of dns:rr/0.
The list must contain a SOA dns:rr/0 to source the zone name and
TTL from as well as as an NSEC3Param dns:rr/0 to source the
hash algorithm, iterations and salt from.
Generate NSEC records from a list of dns:rr/0.
The list must contain a SOA dns:rr/0 which is used to determine zone name and TTL.
Equivalent to gen_nsec(ZoneName, RR, TTL, #{}).
-spec gen_nsec(dns:dname(), [dns:rr()], dns:ttl(), gen_nsec_opts()) -> [dns:rr()].
Generate NSEC records.
-spec ih(nsec3_hashalg() | nsec3_hashalg_fun(), nsec3_salt(), binary(), nsec3_iterations()) -> binary().
NSEC3 iterative hash function.
Equivalent to sign_rr(RR, SignerName, KeyTag, Alg, Key, []).
Signs each RRset in a list of dns:rr/0, as sign_rrset/6 does.
Records form one RRset when their owner names, classes and types are equal, owner names compared
as sign_rrset/6 does, so that case and a final dot don't matter. RRSIG records are left out, as
RFC 4035 §2.2 never signs an RRSIG RRset, and so are the RRsets sign_rrset/6 raises
{badarg, outside_signer_zone} for, which the signer's zone cannot hold, such as a delegation's
NS RRset.
Equivalent to sign_rrset(RRSet, SignerName, KeyTag, Alg, Key, []).
-spec sign_rrset([dns:rr(), ...], dns:dname(), keytag(), sigalg(), key(), sign_rr_opts()) -> dns:rr().
Signs a list of dns:rr/0 of the same class and type.
The validity period is inception and expiration in Opts, by default the current time and a
year after it. Raises:
{badarg, rrsig_rrset}for an RRSIG RRset, which RFC 4035 §2.2 never signs;{badarg, mixed_rrset}for records of more than one RRset, of another owner name, class or type than the first's. Owner names compare as names, so case and a final dot don't matter;{badarg, outside_signer_zone}for an RRset the signer's zone cannot hold, whose RRSIGverify_rrsig/4refuses: an NS, SOA or DNSKEY RRset not at the signer, and so a delegation's NS RRset, which RFC 4035 §2.2 leaves unsigned; an NSEC3 RRset not one label under it; a DS RRset not under it; any other not at it or under it; and an owner with fewer labels than the signer, not counting a leading*, as the apex of a zone whose name starts with*;{badarg, expiration_before_inception}for a period whose expiration is before its inception in serial number arithmetic (RFC 4034 §3.1.5), which reads a period of 2^31 seconds or more that way too, as no RRSIG with one verifies;{badarg, Why}for an RSA or DSA private key whose public key no DNSKEY of the algorithm can carry,Whyasdnskey_public_key/2names it, such as{rsa_modulus_bits, Bits}for an RSA modulus that is not 512 to 4096 bits long, or 1024 to 4096 for RSA/SHA-512;{badarg, key_shape}for an RSA private key of other than 3 numbers,[E, N, D], or 8, with the CRT's after them; and{badarg, number_not_bytes}for one whose numbers are not their big-endian bytes.
None of these holds the key.
-spec verify_rrsig(RRSig :: dns:rr(), RRSet :: [dns:rr()], DNSKeys :: [dns:rr()], Opts :: verify_rrsig_opts()) -> boolean().
Verifies RRSet against the RRSIG record, with one of the DNSKEY records in DNSKeys,
at the time now in Opts, by default the current time.
Only the DNSKEYs the RRSIG names verify it (RFC 4035 §5.3.1): zone keys, with the Zone Key flag
set (RFC 4034 §2.1.1), of protocol 3 and of the RRSIG's class and algorithm, owned by its signer,
whose key tag is the RRSIG's. Names compare in any case and with or without a final dot, and a
key tag is computed from the DNSKEY's RDATA, whatever its keytag field holds.
And only an RRSIG that fits RRSet verifies it (RFC 4035 §5.3.1): every record of RRSet has the
RRSIG's owner and class and the type it covers, and the RRSIG's labels field counts no more
labels than its owner has, not counting a leading * (RFC 4034 §3.1.3), and no fewer than its
signer has. Its signer is a zone that can hold RRSet, as where the zone cuts are is not known
here: the zone at the owner for an NS, SOA or DNSKEY RRset, the zone one label above the owner
for an NSEC3 RRset (RFC 5155 §3), a zone above the owner for a DS RRset (RFC 4034 §5), and the
zone at the owner or above it for any other. Whether that zone holds RRSet, and whether its
DNSKEYs are to be trusted, is for the caller to know.
An RRSIG whose labels field counts fewer labels than its owner has verifies an RRset a wildcard
was expanded into (RFC 4035 §5.3.2), as the wildcard's RRset was signed: owned by * and the
owner's last labels, as many as the field counts. That no name closer to the owner exists, which
NSEC or NSEC3 records prove, is for the caller to check (RFC 4035 §5.3.4).
Returns false when none of those verifies it, when the RRSIG does not fit RRSet, which an
empty RRSet or an owner or signer that is no name never does, when the time is outside the RRSIG's
validity period, and when its algorithm is one this module does not implement. Malformed input
verifies nothing either: an RRSIG whose RDATA did not decode, or a record that is no RRSIG; a key
crypto cannot use, such as an ECDSA key that is no point on its curve; a signature of another
size than its algorithm's, or for RSA than the key's modulus (RFC 8017 §8.2.2); and a record of
RRSet whose RDATA holds a name that is none. Raises {badarg, public_key_not_binary} when a
DNSKEY of the RRSIG's algorithm and signer has a key that is not a binary, as RSA and DSA keys
were before 6.0.0, whether another one verifies it or not.