resolved-dns-dnssec.c revision 5ae5cd4052d85368ec0ca17562d404fa476badc5
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering This file is part of systemd.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering Copyright 2015 Lennart Poettering
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering systemd is free software; you can redistribute it and/or modify it
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering under the terms of the GNU Lesser General Public License as published by
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering (at your option) any later version.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering systemd is distributed in the hope that it will be useful, but
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering Lesser General Public License for more details.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering You should have received a copy of the GNU Lesser General Public License
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering/* Open question:
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering * How does the DNSSEC canonical form of a hostname with a label
b6e676ce41508e2aeea22202fc8f234126177f52Lennart Poettering * containing a dot look like, the way DNS-SD does it?
* - RFC 4035, Section 5.3.4 (When receiving a positive wildcard reply, use NSEC to ensure it actually really applies)
/* Maximum number of NSEC3 iterations we'll do. RFC5155 says 2500 shall be the maximum useful value */
* Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS
static void initialize_libgcrypt(void) {
assert(p);
const uint8_t *p;
size_t i;
if (mask_revoke)
f &= ~DNSKEY_FLAG_REVOKE;
static int rr_compare(const void *a, const void *b) {
size_t m;
assert(x);
assert(*x);
assert(y);
assert(*y);
static int dnssec_rsa_verify_raw(
const char *hash_algorithm,
if (ge != 0) {
r = -EIO;
goto finish;
if (ge != 0) {
r = -EIO;
goto finish;
if (ge != 0) {
r = -EIO;
goto finish;
NULL,
if (ge != 0) {
r = -EIO;
goto finish;
NULL,
(int) data_size,
data);
if (ge != 0) {
r = -EIO;
goto finish;
NULL,
if (ge != 0) {
r = -EIO;
goto finish;
else if (ge != 0) {
r = -EIO;
gcry_mpi_release(e);
gcry_mpi_release(n);
gcry_mpi_release(s);
if (public_key_sexp)
if (signature_sexp)
if (data_sexp)
static int dnssec_rsa_verify(
const char *hash_algorithm,
return -EINVAL;
return -EINVAL;
if (exponent_size <= 0)
return -EINVAL;
return -EINVAL;
return dnssec_rsa_verify_raw(
static int dnssec_ecdsa_verify_raw(
const char *hash_algorithm,
const char *curve,
if (ge != 0) {
k = -EIO;
goto finish;
if (ge != 0) {
k = -EIO;
goto finish;
if (ge != 0) {
k = -EIO;
goto finish;
NULL,
if (ge != 0) {
k = -EIO;
goto finish;
NULL,
(int) data_size,
data);
if (ge != 0) {
k = -EIO;
goto finish;
NULL,
if (ge != 0) {
k = -EIO;
goto finish;
else if (ge != 0) {
k = -EIO;
gcry_mpi_release(r);
gcry_mpi_release(s);
gcry_mpi_release(q);
if (public_key_sexp)
if (signature_sexp)
if (data_sexp)
static int dnssec_ecdsa_verify(
const char *hash_algorithm,
int algorithm,
const char *curve;
uint8_t *q;
return -EOPNOTSUPP;
return -EINVAL;
return -EINVAL;
return dnssec_ecdsa_verify_raw(
v = htobe16(v);
v = htobe32(v);
inception = 0;
switch (algorithm) {
case DNSSEC_ALGORITHM_RSASHA1:
return GCRY_MD_SHA1;
return GCRY_MD_SHA256;
return GCRY_MD_SHA384;
return GCRY_MD_SHA512;
return -EOPNOTSUPP;
int dnssec_verify_rrset(
DnsAnswer *a,
void *hash;
int r, md_algorithm;
bool wildcard = false;
size_t k, n = 0;
if (md_algorithm < 0)
return md_algorithm;
if (n > VERIFY_RRS_MAX)
return -E2BIG;
return -ENODATA;
if (!md)
return -EIO;
goto finish;
const char *suffix;
size_t l;
goto finish;
wildcard = true;
goto finish;
if (!hash) {
r = -EIO;
goto finish;
case DNSSEC_ALGORITHM_RSASHA1:
r = dnssec_rsa_verify(
dnskey);
r = dnssec_ecdsa_verify(
dnskey);
goto finish;
else if (wildcard)
int dnssec_rrsig_match_dnskey(DnsResourceRecord *rrsig, DnsResourceRecord *dnskey, bool revoked_ok) {
return -EINVAL;
static int dnssec_fix_rrset_ttl(DnsAnswer *a, const DnsResourceKey *key, DnsResourceRecord *rrsig, usec_t realtime) {
DnsAnswer *a,
bool found_rrsig = false, found_invalid = false, found_expired_rrsig = false, found_unsupported_algorithm = false;
if (!a || a->n_rrs <= 0)
return -ENODATA;
found_rrsig = true;
switch (one_result) {
case DNSSEC_VALIDATED:
if (ret_rrsig)
case DNSSEC_INVALID:
found_invalid = true;
found_unsupported_algorithm = true;
case DNSSEC_SIGNATURE_EXPIRED:
found_expired_rrsig = true;
if (found_expired_rrsig)
else if (found_unsupported_algorithm)
else if (found_invalid)
else if (found_rrsig)
if (ret_rrsig)
size_t c = 0;
return -ENOBUFS;
return -ENOBUFS;
return -EINVAL;
switch (algorithm) {
case DNSSEC_DIGEST_SHA1:
return GCRY_MD_SHA1;
case DNSSEC_DIGEST_SHA256:
return GCRY_MD_SHA256;
case DNSSEC_DIGEST_SHA384:
return GCRY_MD_SHA384;
return -EOPNOTSUPP;
int md_algorithm, r;
void *result;
return -EINVAL;
return -EINVAL;
return -EKEYREJECTED;
return -EKEYREJECTED;
return -EKEYREJECTED;
if (md_algorithm < 0)
return md_algorithm;
if (!md)
return -EIO;
if (mask_revoke)
if (!result) {
r = -EIO;
goto finish;
if (r == -EKEYREJECTED)
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_format(const uint8_t *hashed, size_t hashed_size, const char *zone, char **ret) {
return -ENOMEM;
return -ENOMEM;
*ret = j;
return (int) hashed_size;
static int nsec3_hashed_domain_make(DnsResourceRecord *nsec3, const char *domain, const char *zone, char **ret) {
int hashed_size;
if (hashed_size < 0)
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, uint32_t *ttl) {
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) {
if (authenticated)
*authenticated = a;
if (ttl)
if (!wildcard)
return -ENOMEM;
if (r != hashed_size)
return -EBADMSG;
if (r != hashed_size)
return -EBADMSG;
r = nsec3_hashed_domain_format(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, zone, &next_hashed_domain);
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)
if (authenticated)
*authenticated = a;
if (ttl)
int dnssec_nsec_test(DnsAnswer *answer, DnsResourceKey *key, DnssecNsecResult *result, bool *authenticated, uint32_t *ttl) {
bool have_nsec3 = false;
case DNS_TYPE_NSEC:
if (authenticated)
if (ttl)
r = dns_name_between(DNS_RESOURCE_KEY_NAME(rr->key), DNS_RESOURCE_KEY_NAME(key), rr->nsec.next_domain_name);
if (authenticated)
if (ttl)
case DNS_TYPE_NSEC3:
have_nsec3 = true;
if (have_nsec3)
int dnssec_nsec_test_between(DnsAnswer *answer, const char *name, const char *zone, bool *authenticated) {
/* Checks whether there's an NSEC/NSEC3 that proves that the specified 'name' is non-existing in the specified
bool found = false;
case DNS_TYPE_NSEC:
found = r > 0;
case DNS_TYPE_NSEC3: {
rr,
name,
zone,
zone,
found = r > 0;
if (found) {
if (authenticated)