resolved-dns-trust-anchor.c revision 105f6c4bdcdd9c7233370f1bc143913d5ab0d099
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering This file is part of systemd.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Copyright 2015 Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering systemd is free software; you can redistribute it and/or modify it
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering under the terms of the GNU Lesser General Public License as published by
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering (at your option) any later version.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering systemd is distributed in the hope that it will be useful, but
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering Lesser General Public License for more details.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering You should have received a copy of the GNU Lesser General Public License
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic const char trust_anchor_dirs[] = CONF_PATHS_NULSTR("dnssec-trust-anchors.d");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering/* The DS RR from https://data.iana.org/root-anchors/root-anchors.xml, retrieved December 2015 */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering { 0x49, 0xAA, 0xC1, 0x1D, 0x7B, 0x6F, 0x64, 0x46, 0x70, 0x2E, 0x54, 0xA1, 0x60, 0x73, 0x71, 0x60,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering 0x7A, 0x1A, 0x41, 0x85, 0x52, 0x00, 0xFD, 0x2C, 0xE1, 0xCD, 0xDE, 0x32, 0xF2, 0x4E, 0x8F, 0xB5 };
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int dns_trust_anchor_add_builtin(DnsTrustAnchor *d) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (hashmap_get(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DS, ".")))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (hashmap_get(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DNSKEY, ".")))
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Add the RR from https://data.iana.org/root-anchors/root-anchors.xml */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, "");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rr->ds.algorithm = DNSSEC_ALGORITHM_RSASHA256;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rr->ds.digest_type = DNSSEC_DIGEST_SHA256;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering rr->ds.digest_size = sizeof(root_digest);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering rr->ds.digest = memdup(root_digest, rr->ds.digest_size);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r = hashmap_put(d->positive_by_key, rr->key, answer);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int dns_trust_anchor_load_positive(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering _cleanup_free_ char *domain = NULL, *class = NULL, *type = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *p = s;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = extract_first_word(&p, &domain, NULL, EXTRACT_QUOTES);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_warning_errno(r, "Unable to parse domain in line %s:%u: %m", path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = extract_many_words(&p, NULL, 0, &class, &type, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_warning_errno(r, "Unable to parse class and type in line %s:%u: %m", path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning("Missing class or type in line %s:%u", path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning("RR class %s is not supported, ignoring line %s:%u.", class, path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering _cleanup_free_ char *key_tag = NULL, *algorithm = NULL, *digest_type = NULL, *digest = NULL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = extract_many_words(&p, NULL, 0, &key_tag, &algorithm, &digest_type, &digest, NULL);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning_errno(r, "Failed to parse DS parameters on line %s:%u: %m", path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning("Missing DS parameters on line %s:%u", path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_warning_errno(r, "Failed to parse DS key tag %s on line %s:%u: %m", key_tag, path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering a = dnssec_algorithm_from_string(algorithm);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning("Failed to parse DS algorithm %s on line %s:%u", algorithm, path, line);
0f7091e624fdba6c0bf281f2a9a23cd3e9ca93fbLennart Poettering dt = dnssec_digest_from_string(digest_type);
0f7091e624fdba6c0bf281f2a9a23cd3e9ca93fbLennart Poettering log_warning("Failed to parse DS digest type %s on line %s:%u", digest_type, path, line);
0f7091e624fdba6c0bf281f2a9a23cd3e9ca93fbLennart Poettering r = unhexmem(digest, strlen(digest), &dd, &l);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning("Failed to parse DS digest %s on line %s:%u", digest, path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, domain);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering _cleanup_free_ char *flags = NULL, *protocol = NULL, *algorithm = NULL, *key = NULL;
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering r = extract_many_words(&p, NULL, 0, &flags, &protocol, &algorithm, &key, NULL);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering return log_warning_errno(r, "Failed to parse DNSKEY parameters on line %s:%u: %m", path, line);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering log_warning("Missing DNSKEY parameters on line %s:%u", path, line);
8013e860b6344cb109e68208a3a91b0fc3cb9ed1Lennart Poettering log_warning("DNSKEY Protocol is not 3 on line %s:%u", path, line);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering return log_warning_errno(r, "Failed to parse DNSKEY flags field %s on line %s:%u", flags, path, line);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering log_warning("DNSKEY lacks zone key bit set on line %s:%u", path, line);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering log_warning("DNSKEY is already revoked on line %s:%u", path, line);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering a = dnssec_algorithm_from_string(algorithm);
1086182d83d4c02a75f96f0184d5e8e5d3af6528Lennart Poettering log_warning("Failed to parse DNSKEY algorithm %s on line %s:%u", algorithm, path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = unbase64mem(key, strlen(key), &k, &l);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_warning_errno(r, "Failed to parse DNSKEY key data %s on line %s:%u", key, path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, domain);
8013e860b6344cb109e68208a3a91b0fc3cb9ed1Lennart Poettering log_warning("RR type %s is not supported, ignoring line %s:%u.", type, path, line);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering log_warning("Trailing garbage on line %s:%u, ignoring line.", path, line);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering old_answer = hashmap_get(d->positive_by_key, rr->key);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering r = dns_answer_add_extend(&answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return log_error_errno(r, "Failed to add trust anchor RR: %m");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = hashmap_replace(d->positive_by_key, rr->key, answer);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_error_errno(r, "Failed to add answer to trust anchor: %m");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering old_answer = dns_answer_unref(old_answer);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int dns_trust_anchor_load_negative(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering const char *p = s;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = extract_first_word(&p, &domain, NULL, EXTRACT_QUOTES);
34b9656f0b2890743eee6a746ef08d817abfd5e9Lennart Poettering return log_warning_errno(r, "Unable to parse line %s:%u: %m", path, line);
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_warning("Trailing garbage at line %s:%u, ignoring line.", path, line);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = set_put(d->negative_by_name, domain);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering int (*loader)(DnsTrustAnchor *d, const char *path, unsigned n, const char *line)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = conf_files_list_nulstr(&files, suffix, NULL, trust_anchor_dirs);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return log_error_errno(r, "Failed to enumerate %s trust anchor files: %m", suffix);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering unsigned n = 0;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_warning_errno(errno, "Failed to open %s: %m", *f);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering FOREACH_LINE(line, g, log_warning_errno(errno, "Failed to read %s, ignoring: %m", *f)) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (*l == ';')
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering (void) loader(d, *f, n, l);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic void dns_trust_anchor_dump(DnsTrustAnchor *d) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_info("No positive trust anchors defined.");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering HASHMAP_FOREACH(a, d->positive_by_key, i) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_info("%s", dns_resource_record_to_string(rr));
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_info("No negative trust anchors defined.");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_info("%s%s", n, endswith(n, ".") ? "" : ".");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringint dns_trust_anchor_load(DnsTrustAnchor *d) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* If loading things from disk fails, we don't consider this fatal */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering (void) dns_trust_anchor_load_files(d, ".positive", dns_trust_anchor_load_positive);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering (void) dns_trust_anchor_load_files(d, ".negative", dns_trust_anchor_load_negative);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* However, if the built-in DS fails, then we have a problem. */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering return log_error_errno(r, "Failed to add trust anchor built-in: %m");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringvoid dns_trust_anchor_flush(DnsTrustAnchor *d) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering while ((a = hashmap_steal_first(d->positive_by_key)))
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering d->positive_by_key = hashmap_free(d->positive_by_key);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering d->negative_by_name = set_free_free(d->negative_by_name);
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringint dns_trust_anchor_lookup_positive(DnsTrustAnchor *d, const DnsResourceKey *key, DnsAnswer **ret) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* We only serve DS and DNSKEY RRs. */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!IN_SET(key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
assert(d);
if (!old_answer)
LOG_MESSAGE("DNSSEC Trust anchor %s has been revoked. Please update the trust anchor, or upgrade your operating system."), strna(dns_resource_record_to_string(rr)),
NULL);
static int dns_trust_anchor_check_revoked_one(DnsTrustAnchor *d, DnsResourceRecord *revoked_dnskey) {
DnsAnswer *a;
assert(d);
if (((anchor->dnskey.flags ^ revoked_dnskey->dnskey.flags) | DNSKEY_FLAG_REVOKE) != DNSKEY_FLAG_REVOKE)
a = hashmap_get(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(revoked_dnskey->key->class, DNS_TYPE_DS, DNS_RESOURCE_KEY_NAME(revoked_dnskey->key)));
assert(d);
hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DNSKEY, name)) ||
assert(d);