resolved-dns-dnssec.c revision 6f8a2c6817e35ca3e76130b31624f7f30e596433
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
199767f8919635c4928607450d9e0abb932109ceToomas Soome This file is part of systemd.
199767f8919635c4928607450d9e0abb932109ceToomas Soome Copyright 2015 Lennart Poettering
199767f8919635c4928607450d9e0abb932109ceToomas Soome systemd is free software; you can redistribute it and/or modify it
199767f8919635c4928607450d9e0abb932109ceToomas Soome under the terms of the GNU Lesser General Public License as published by
199767f8919635c4928607450d9e0abb932109ceToomas Soome the Free Software Foundation; either version 2.1 of the License, or
199767f8919635c4928607450d9e0abb932109ceToomas Soome (at your option) any later version.
199767f8919635c4928607450d9e0abb932109ceToomas Soome systemd is distributed in the hope that it will be useful, but
199767f8919635c4928607450d9e0abb932109ceToomas Soome WITHOUT ANY WARRANTY; without even the implied warranty of
199767f8919635c4928607450d9e0abb932109ceToomas Soome MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
199767f8919635c4928607450d9e0abb932109ceToomas Soome Lesser General Public License for more details.
199767f8919635c4928607450d9e0abb932109ceToomas Soome You should have received a copy of the GNU Lesser General Public License
199767f8919635c4928607450d9e0abb932109ceToomas Soome along with systemd; If not, see <http://www.gnu.org/licenses/>.
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Open question:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * How does the DNSSEC canonical form of a hostname with a label
199767f8919635c4928607450d9e0abb932109ceToomas Soome * containing a dot look like, the way DNS-SD does it?
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - wildcard zones compatibility (NSEC/NSEC3 wildcard check is missing)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - multi-label zone compatibility
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - cname/dname compatibility
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - nxdomain on qname
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - per-interface DNSSEC setting
199767f8919635c4928607450d9e0abb932109ceToomas Soome * - when doing negative caching, use NSEC/NSEC3 RR instead of SOA for TTL
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Permit a maximum clock skew of 1h 10min. This should be enough to deal with DST confusion */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define SKEW_MAX (1*USEC_PER_HOUR + 10*USEC_PER_MINUTE)
199767f8919635c4928607450d9e0abb932109ceToomas Soome/* Maximum number of NSEC3 iterations we'll do. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The DNSSEC Chain of trust:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Normal RRs are protected via RRSIG RRs in combination with DNSKEY RRs, all in the same zone
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DNSKEY RRs are either protected like normal RRs, or via a DS from a zone "higher" up the tree
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DS RRs are protected like normal RRs
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Example chain:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void initialize_libgcrypt(void) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char *p;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (gcry_control(GCRYCTL_INITIALIZATION_FINISHED_P))
199767f8919635c4928607450d9e0abb932109ceToomas Soome gcry_control(GCRYCTL_INITIALIZATION_FINISHED, 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuint16_t dnssec_keytag(DnsResourceRecord *dnskey, bool mask_revoke) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The algorithm from RFC 4034, Appendix B. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sum = f + ((((uint32_t) dnskey->dnskey.protocol) << 8) + (uint32_t) dnskey->dnskey.algorithm);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sum += (i & 1) == 0 ? (uint32_t) p[i] << 8 : (uint32_t) p[i];
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int rr_compare(const void *a, const void *b) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome DnsResourceRecord **x = (DnsResourceRecord**) a, **y = (DnsResourceRecord**) b;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Let's order the RRs according to RFC 4034, Section 6.3 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome m = MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(*x), DNS_RESOURCE_RECORD_RDATA_SIZE(*y));
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = memcmp(DNS_RESOURCE_RECORD_RDATA(*x), DNS_RESOURCE_RECORD_RDATA(*y), m);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x) < DNS_RESOURCE_RECORD_RDATA_SIZE(*y))
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x) > DNS_RESOURCE_RECORD_RDATA_SIZE(*y))
199767f8919635c4928607450d9e0abb932109ceToomas Soome gcry_sexp_t public_key_sexp = NULL, data_sexp = NULL, signature_sexp = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ge = gcry_mpi_scan(&s, GCRYMPI_FMT_USG, signature, signature_size, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ge = gcry_mpi_scan(&e, GCRYMPI_FMT_USG, exponent, exponent_size, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ge = gcry_mpi_scan(&n, GCRYMPI_FMT_USG, modulus, modulus_size, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome "(sig-val (rsa (s %m)))",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "(data (flags pkcs1) (hash %s %b))",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "(public-key (rsa (n %m) (e %m)))",
199767f8919635c4928607450d9e0abb932109ceToomas Soome ge = gcry_pk_verify(signature_sexp, data_sexp, public_key_sexp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (ge != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome log_debug("RSA signature check failed: %s", gpg_strerror(ge));
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* exponent is > 255 bytes long */
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((size_t) (((uint8_t*) dnskey->dnskey.key)[1]) << 8) |
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (3 + exponent_size >= dnskey->dnskey.key_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome modulus = (uint8_t*) dnskey->dnskey.key + 3 + exponent_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome modulus_size = dnskey->dnskey.key_size - 3 - exponent_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* exponent is <= 255 bytes long */
199767f8919635c4928607450d9e0abb932109ceToomas Soome exponent_size = (size_t) ((uint8_t*) dnskey->dnskey.key)[0];
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (1 + exponent_size >= dnskey->dnskey.key_size)
199767f8919635c4928607450d9e0abb932109ceToomas Soome modulus = (uint8_t*) dnskey->dnskey.key + 1 + exponent_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome modulus_size = dnskey->dnskey.key_size - 1 - exponent_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome rrsig->rrsig.signature, rrsig->rrsig.signature_size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome const void *signature_r, size_t signature_r_size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome const void *signature_s, size_t signature_s_size,
199767f8919635c4928607450d9e0abb932109ceToomas Soome gcry_sexp_t public_key_sexp = NULL, data_sexp = NULL, signature_sexp = NULL;
199767f8919635c4928607450d9e0abb932109ceToomas Soome ge = gcry_mpi_scan(&r, GCRYMPI_FMT_USG, signature_r, signature_r_size, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ge = gcry_mpi_scan(&s, GCRYMPI_FMT_USG, signature_s, signature_s_size, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome ge = gcry_mpi_scan(&q, GCRYMPI_FMT_USG, key, key_size, NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome "(sig-val (ecdsa (r %m) (s %m)))",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "(data (flags rfc6979) (hash %s %b))",
199767f8919635c4928607450d9e0abb932109ceToomas Soome "(public-key (ecc (curve %s) (q %m)))",
199767f8919635c4928607450d9e0abb932109ceToomas Soome ge = gcry_pk_verify(signature_sexp, data_sexp, public_key_sexp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome else if (ge != 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome log_debug("ECDSA signature check failed: %s", gpg_strerror(ge));
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (algorithm == DNSSEC_ALGORITHM_ECDSAP256SHA256) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome } else if (algorithm == DNSSEC_ALGORITHM_ECDSAP384SHA384) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rrsig->rrsig.signature_size != key_size * 2)
199767f8919635c4928607450d9e0abb932109ceToomas Soome q[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
199767f8919635c4928607450d9e0abb932109ceToomas Soome (uint8_t*) rrsig->rrsig.signature + key_size, key_size,
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void md_add_uint8(gcry_md_hd_t md, uint8_t v) {
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void md_add_uint16(gcry_md_hd_t md, uint16_t v) {
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic void md_add_uint32(gcry_md_hd_t md, uint32_t v) {
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int dnssec_rrsig_expired(DnsResourceRecord *rrsig, usec_t realtime) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome expiration = rrsig->rrsig.expiration * USEC_PER_SEC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome inception = rrsig->rrsig.inception * USEC_PER_SEC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Permit a certain amount of clock skew of 10% of the valid
199767f8919635c4928607450d9e0abb932109ceToomas Soome * time range. This takes inspiration from unbound's
199767f8919635c4928607450d9e0abb932109ceToomas Soome * resolver. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return realtime < inception || realtime > expiration;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int algorithm_to_gcrypt_md(uint8_t algorithm) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Translates a DNSSEC signature algorithm into a gcrypt
199767f8919635c4928607450d9e0abb932109ceToomas Soome * digest identifier.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Note that we implement all algorithms listed as "Must
199767f8919635c4928607450d9e0abb932109ceToomas Soome * implement" and "Recommended to Implement" in RFC6944. We
199767f8919635c4928607450d9e0abb932109ceToomas Soome * don't implement any algorithms that are listed as
199767f8919635c4928607450d9e0abb932109ceToomas Soome * "Optional" or "Must Not Implement". Specifically, we do not
199767f8919635c4928607450d9e0abb932109ceToomas Soome * implement RSAMD5, DSASHA1, DH, DSA-NSEC3-SHA1, and
199767f8919635c4928607450d9e0abb932109ceToomas Soome * GOST-ECC. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint8_t wire_format_name[DNS_WIRE_FOMAT_HOSTNAME_MAX];
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Verifies the the RRSet matching the specified "key" in "a",
199767f8919635c4928607450d9e0abb932109ceToomas Soome * using the signature "rrsig" and the key "dnskey". It's
199767f8919635c4928607450d9e0abb932109ceToomas Soome * assumed the RRSIG and DNSKEY match. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome md_algorithm = algorithm_to_gcrypt_md(rrsig->rrsig.algorithm);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (r > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Collect all relevant RRs in a single array, so that we can look at the RRset */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* We need the wire format for ordering, and digest calculation */
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dns_resource_record_to_wire_format(rr, true);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Bring the RRs into canonical order */
199767f8919635c4928607450d9e0abb932109ceToomas Soome qsort_safe(list, n, sizeof(DnsResourceRecord*), rr_compare);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* OK, the RRs are now in canonical order. Let's calculate the digest */
199767f8919635c4928607450d9e0abb932109ceToomas Soome hash_size = gcry_md_get_algo_dlen(md_algorithm);
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dns_name_to_wire_format(rrsig->rrsig.signer, wire_format_name, sizeof(wire_format_name), true);
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (k = 0; k < n; k++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dns_name_suffix(DNS_RESOURCE_KEY_NAME(rr->key), rrsig->rrsig.labels, &suffix);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (r > 0) /* This is a wildcard! */
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dns_name_to_wire_format(suffix, wire_format_name, sizeof(wire_format_name), true);
199767f8919635c4928607450d9e0abb932109ceToomas Soome gcry_md_write(md, DNS_RESOURCE_RECORD_RDATA(rr), l);
199767f8919635c4928607450d9e0abb932109ceToomas Soome *result = r ? DNSSEC_VALIDATED : DNSSEC_INVALID;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint dnssec_rrsig_match_dnskey(DnsResourceRecord *rrsig, DnsResourceRecord *dnskey, bool revoked_ok) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Checks if the specified DNSKEY RR matches the key used for
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the signature in the specified RRSIG RR */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((dnskey->dnskey.flags & DNSKEY_FLAG_ZONE_KEY) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!revoked_ok && (dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE))
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dnskey->dnskey.algorithm != rrsig->rrsig.algorithm)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dnssec_keytag(dnskey, false) != rrsig->rrsig.key_tag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return dns_name_equal(DNS_RESOURCE_KEY_NAME(dnskey->key), rrsig->rrsig.signer);
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint dnssec_key_match_rrsig(const DnsResourceKey *key, DnsResourceRecord *rrsig) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Checks if the specified RRSIG RR protects the RRSet of the specified RR key. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Make sure signer is a parent of the RRset */
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dns_name_endswith(DNS_RESOURCE_KEY_NAME(rrsig->key), rrsig->rrsig.signer);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Make sure the owner name has at least as many labels as the "label" fields indicates. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dns_name_count_labels(DNS_RESOURCE_KEY_NAME(rrsig->key));
199767f8919635c4928607450d9e0abb932109ceToomas Soome return dns_name_equal(DNS_RESOURCE_KEY_NAME(rrsig->key), DNS_RESOURCE_KEY_NAME(key));
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int dnssec_fix_rrset_ttl(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord *rrsig, usec_t realtime) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Pick the TTL as the minimum of the RR's TTL, the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RR's original TTL according to the RRSIG and the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * RRSIG's own TTL, see RFC 4035, Section 5.3.3 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome rr->ttl = MIN3(rr->ttl, rrsig->rrsig.original_ttl, rrsig->ttl);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rr->expiry = rrsig->rrsig.expiration * USEC_PER_SEC;
199767f8919635c4928607450d9e0abb932109ceToomas Soome bool found_rrsig = false, found_invalid = false, found_expired_rrsig = false, found_unsupported_algorithm = false;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Verifies all RRs from "a" that match the key "key" against DNSKEYs in "validated_dnskeys" */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!a || a->n_rrs <= 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Iterate through each RRSIG RR. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Is this an RRSIG RR that applies to RRs matching our key? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Look for a matching key */
199767f8919635c4928607450d9e0abb932109ceToomas Soome DNS_ANSWER_FOREACH_FLAGS(dnskey, flags, validated_dnskeys) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Is this a DNSKEY RR that matches they key of our RRSIG? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dnssec_rrsig_match_dnskey(rrsig, dnskey, false);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Take the time here, if it isn't set yet, so
199767f8919635c4928607450d9e0abb932109ceToomas Soome * that we do all validations with the same
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Yay, we found a matching RRSIG with a matching
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DNSKEY, awesome. Now let's verify all entries of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the RRSet against the RRSIG and DNSKEY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * combination. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dnssec_verify_rrset(a, key, rrsig, dnskey, realtime, &one_result);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Yay, the RR has been validated,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * return immediately, but fix up the expiry */
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dnssec_fix_rrset_ttl(a, key, rrsig, realtime);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If the signature is invalid, let's try another
199767f8919635c4928607450d9e0abb932109ceToomas Soome key and/or signature. After all they
199767f8919635c4928607450d9e0abb932109ceToomas Soome key_tags and stuff are not unique, and
199767f8919635c4928607450d9e0abb932109ceToomas Soome might be shared by multiple keys. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If the key algorithm is
199767f8919635c4928607450d9e0abb932109ceToomas Soome unsupported, try another
199767f8919635c4928607450d9e0abb932109ceToomas Soome RRSIG/DNSKEY pair, but remember we
199767f8919635c4928607450d9e0abb932109ceToomas Soome encountered this, so that we can
199767f8919635c4928607450d9e0abb932109ceToomas Soome return a proper error when we
199767f8919635c4928607450d9e0abb932109ceToomas Soome encounter nothing better. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* If the signature is expired, try
199767f8919635c4928607450d9e0abb932109ceToomas Soome another one, but remember it, so
199767f8919635c4928607450d9e0abb932109ceToomas Soome that we can return this */
199767f8919635c4928607450d9e0abb932109ceToomas Soome assert_not_reached("Unexpected DNSSEC validation result");
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint dnssec_has_rrsig(DnsAnswer *a, const DnsResourceKey *key) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Checks whether there's at least one RRSIG in 'a' that proctects RRs of the specified key */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint dnssec_canonicalize(const char *n, char *buffer, size_t buffer_max) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Converts the specified hostname into DNSSEC canonicalized
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dns_label_unescape(&n, buffer, buffer_max);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (r > 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* DNSSEC validation is always done on the ASCII version of the label */
199767f8919635c4928607450d9e0abb932109ceToomas Soome k = dns_label_apply_idna(buffer, r, buffer, buffer_max);
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* The DNSSEC canonical form is not clear on what to
199767f8919635c4928607450d9e0abb932109ceToomas Soome * do with dots appearing in labels, the way DNS-SD
199767f8919635c4928607450d9e0abb932109ceToomas Soome * does it. Refuse it for now. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i = 0; i < (size_t) r; i ++) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (c <= 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Not even a single label: this is the root domain name */
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (int) c;
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int digest_to_gcrypt_md(uint8_t algorithm) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Translates a DNSSEC digest algorithm into a gcrypt digest identifier */
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint dnssec_verify_dnskey(DnsResourceRecord *dnskey, DnsResourceRecord *ds, bool mask_revoke) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome char owner_name[DNSSEC_CANONICAL_HOSTNAME_MAX];
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Implements DNSKEY verification by a DS, according to RFC 4035, section 5.2 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((dnskey->dnskey.flags & DNSKEY_FLAG_ZONE_KEY) == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!mask_revoke && (dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE))
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dnskey->dnskey.algorithm != ds->ds.algorithm)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (dnssec_keytag(dnskey, mask_revoke) != ds->ds.key_tag)
199767f8919635c4928607450d9e0abb932109ceToomas Soome md_algorithm = digest_to_gcrypt_md(ds->ds.digest_type);
199767f8919635c4928607450d9e0abb932109ceToomas Soome hash_size = gcry_md_get_algo_dlen(md_algorithm);
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dnssec_canonicalize(DNS_RESOURCE_KEY_NAME(dnskey->key), owner_name, sizeof(owner_name));
199767f8919635c4928607450d9e0abb932109ceToomas Soome md_add_uint16(md, dnskey->dnskey.flags & ~DNSKEY_FLAG_REVOKE);
199767f8919635c4928607450d9e0abb932109ceToomas Soome gcry_md_write(md, dnskey->dnskey.key, dnskey->dnskey.key_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = memcmp(result, ds->ds.digest, ds->ds.digest_size) != 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint dnssec_verify_dnskey_search(DnsResourceRecord *dnskey, DnsAnswer *validated_ds) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome DNS_ANSWER_FOREACH_FLAGS(ds, flags, validated_ds) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome r = dns_name_equal(DNS_RESOURCE_KEY_NAME(dnskey->key), DNS_RESOURCE_KEY_NAME(ds->key));
switch (algorithm) {
case NSEC3_ALGORITHM_SHA1:
return GCRY_MD_SHA1;
return -EOPNOTSUPP;
int algorithm;
void *result;
return -EINVAL;
log_debug("Ignoring NSEC3 RR %s with excessive number of iterations.", dns_resource_record_to_string(nsec3));
return -EOPNOTSUPP;
if (algorithm < 0)
return algorithm;
return -EINVAL;
if (!md)
return -EIO;
if (!result) {
r = -EIO;
goto finish;
if (!result) {
r = -EIO;
goto finish;
r = (int) hash_size;
if (!nsec3)
return dns_name_equal(a, b);
static int nsec3_hashed_domain(DnsResourceRecord *nsec3, const char *domain, const char *zone, char **ret) {
int hashed_size;
if (hashed_size < 0)
return hashed_size;
return -ENOMEM;
if (!hashed_domain)
return -ENOMEM;
return hashed_size;
* First try to find a NSEC3 record that matches our query precisely, if that fails, find the closest
* is no wildcard domain as a direct descendant of the closest enclosure, or find an NSEC3 record that
* that there is no proof either way. The latter is the case if a the proof of non-existence of a given
* name uses an NSEC3 record with the opt-out bit set. Lastly, if we are given insufficient NSEC3 records
static int dnssec_test_nsec3(DnsAnswer *answer, DnsResourceKey *key, DnssecNsecResult *result, bool *authenticated) {
int hashed_size, r;
goto found_zone;
if (hashed_size < 0)
return hashed_size;
goto found_closest_encloser;
pp = p;
r = dns_name_parent(&p);
return -EBADMSG;
return -EBADMSG;
if (!pp) {
*authenticated = a;
if (!wildcard)
return -ENOMEM;
if (r != hashed_size)
return -EBADMSG;
if (r != hashed_size)
return -EBADMSG;
if (!label)
return -ENOMEM;
if (!next_hashed_domain)
return -ENOMEM;
optout = true;
no_closer = true;
optout = true;
no_wildcard = true;
return -EBADMSG;
if (!no_closer) {
if (wildcard_rr) {
if (optout)
if (optout)
else if (no_wildcard)
*authenticated = a;
int dnssec_test_nsec(DnsAnswer *answer, DnsResourceKey *key, DnssecNsecResult *result, bool *authenticated) {
bool have_nsec3 = false;
case DNS_TYPE_NSEC:
r = dns_name_between(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key), rr->nsec.next_domain_name);
case DNS_TYPE_NSEC3:
have_nsec3 = true;
if (have_nsec3)