2 This file is part of systemd.
4 Copyright 2015 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <sd-messages.h>
22 #include "alloc-util.h"
23 #include "conf-files.h"
25 #include "dns-domain.h"
28 #include "hexdecoct.h"
29 #include "parse-util.h"
30 #include "resolved-dns-trust-anchor.h"
31 #include "resolved-dns-dnssec.h"
33 #include "string-util.h"
36 static const char trust_anchor_dirs
[] = CONF_PATHS_NULSTR("dnssec-trust-anchors.d");
38 /* The DS RR from https://data.iana.org/root-anchors/root-anchors.xml, retrieved December 2015 */
39 static const uint8_t root_digest
[] =
40 { 0x49, 0xAA, 0xC1, 0x1D, 0x7B, 0x6F, 0x64, 0x46, 0x70, 0x2E, 0x54, 0xA1, 0x60, 0x73, 0x71, 0x60,
41 0x7A, 0x1A, 0x41, 0x85, 0x52, 0x00, 0xFD, 0x2C, 0xE1, 0xCD, 0xDE, 0x32, 0xF2, 0x4E, 0x8F, 0xB5 };
43 static bool dns_trust_anchor_knows_domain_positive(DnsTrustAnchor
*d
, const char *name
) {
46 /* Returns true if there's an entry for the specified domain
47 * name in our trust anchor */
50 hashmap_contains(d
->positive_by_key
, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_DNSKEY
, name
)) ||
51 hashmap_contains(d
->positive_by_key
, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN
, DNS_TYPE_DS
, name
));
54 static int dns_trust_anchor_add_builtin_positive(DnsTrustAnchor
*d
) {
55 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
56 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
61 r
= hashmap_ensure_allocated(&d
->positive_by_key
, &dns_resource_key_hash_ops
);
65 /* Only add the built-in trust anchor if there's neither a DS
66 * nor a DNSKEY defined for the root domain. That way users
67 * have an easy way to override the root domain DS/DNSKEY
69 if (dns_trust_anchor_knows_domain_positive(d
, "."))
72 /* Add the RR from https://data.iana.org/root-anchors/root-anchors.xml */
73 rr
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_DS
, "");
77 rr
->ds
.key_tag
= 19036;
78 rr
->ds
.algorithm
= DNSSEC_ALGORITHM_RSASHA256
;
79 rr
->ds
.digest_type
= DNSSEC_DIGEST_SHA256
;
80 rr
->ds
.digest_size
= sizeof(root_digest
);
81 rr
->ds
.digest
= memdup(root_digest
, rr
->ds
.digest_size
);
85 answer
= dns_answer_new(1);
89 r
= dns_answer_add(answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
93 r
= hashmap_put(d
->positive_by_key
, rr
->key
, answer
);
101 static int dns_trust_anchor_add_builtin_negative(DnsTrustAnchor
*d
) {
103 static const char private_domains
[] =
104 /* RFC 6761 says that .test is a special domain for
105 * testing and not to be installed in the root zone */
108 /* RFC 6761 says that these reverse IP lookup ranges
109 * are for private addresses, and hence should not
110 * show up in the root zone */
112 "16.172.in-addr.arpa\0"
113 "17.172.in-addr.arpa\0"
114 "18.172.in-addr.arpa\0"
115 "19.172.in-addr.arpa\0"
116 "20.172.in-addr.arpa\0"
117 "21.172.in-addr.arpa\0"
118 "22.172.in-addr.arpa\0"
119 "23.172.in-addr.arpa\0"
120 "24.172.in-addr.arpa\0"
121 "25.172.in-addr.arpa\0"
122 "26.172.in-addr.arpa\0"
123 "27.172.in-addr.arpa\0"
124 "28.172.in-addr.arpa\0"
125 "29.172.in-addr.arpa\0"
126 "30.172.in-addr.arpa\0"
127 "31.172.in-addr.arpa\0"
128 "168.192.in-addr.arpa\0"
130 /* RFC 6762 reserves the .local domain for Multicast
131 * DNS, it hence cannot appear in the root zone. (Note
132 * that we by default do not route .local traffic to
133 * DNS anyway, except when a configured search domain
137 /* These two are well known, popular private zone
138 * TLDs, that are blocked from delegation, according
140 * http://icannwiki.com/Name_Collision#NGPC_Resolution
142 * There's also ongoing work on making this official
144 * https://www.ietf.org/archive/id/draft-chapin-additional-reserved-tlds-02.txt */
148 /* The following four TLDs are suggested for private
149 * zones in RFC 6762, Appendix G, and are hence very
150 * unlikely to be made official TLDs any day soon */
161 /* Only add the built-in trust anchor if there's no negative
162 * trust anchor defined at all. This enables easy overriding
163 * of negative trust anchors. */
165 if (set_size(d
->negative_by_name
) > 0)
168 r
= set_ensure_allocated(&d
->negative_by_name
, &dns_name_hash_ops
);
172 /* We add a couple of domains as default negative trust
173 * anchors, where it's very unlikely they will be installed in
174 * the root zone. If they exist they must be private, and thus
177 NULSTR_FOREACH(name
, private_domains
) {
179 if (dns_trust_anchor_knows_domain_positive(d
, name
))
182 r
= set_put_strdup(d
->negative_by_name
, name
);
190 static int dns_trust_anchor_load_positive(DnsTrustAnchor
*d
, const char *path
, unsigned line
, const char *s
) {
191 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
192 _cleanup_free_
char *domain
= NULL
, *class = NULL
, *type
= NULL
;
193 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
194 DnsAnswer
*old_answer
= NULL
;
201 r
= extract_first_word(&p
, &domain
, NULL
, EXTRACT_QUOTES
);
203 return log_warning_errno(r
, "Unable to parse domain in line %s:%u: %m", path
, line
);
205 if (!dns_name_is_valid(domain
)) {
206 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain
, path
, line
);
210 r
= extract_many_words(&p
, NULL
, 0, &class, &type
, NULL
);
212 return log_warning_errno(r
, "Unable to parse class and type in line %s:%u: %m", path
, line
);
214 log_warning("Missing class or type in line %s:%u", path
, line
);
218 if (!strcaseeq(class, "IN")) {
219 log_warning("RR class %s is not supported, ignoring line %s:%u.", class, path
, line
);
223 if (strcaseeq(type
, "DS")) {
224 _cleanup_free_
char *key_tag
= NULL
, *algorithm
= NULL
, *digest_type
= NULL
, *digest
= NULL
;
225 _cleanup_free_
void *dd
= NULL
;
230 r
= extract_many_words(&p
, NULL
, 0, &key_tag
, &algorithm
, &digest_type
, &digest
, NULL
);
232 log_warning_errno(r
, "Failed to parse DS parameters on line %s:%u: %m", path
, line
);
236 log_warning("Missing DS parameters on line %s:%u", path
, line
);
240 r
= safe_atou16(key_tag
, &kt
);
242 return log_warning_errno(r
, "Failed to parse DS key tag %s on line %s:%u: %m", key_tag
, path
, line
);
244 a
= dnssec_algorithm_from_string(algorithm
);
246 log_warning("Failed to parse DS algorithm %s on line %s:%u", algorithm
, path
, line
);
250 dt
= dnssec_digest_from_string(digest_type
);
252 log_warning("Failed to parse DS digest type %s on line %s:%u", digest_type
, path
, line
);
256 r
= unhexmem(digest
, strlen(digest
), &dd
, &l
);
258 log_warning("Failed to parse DS digest %s on line %s:%u", digest
, path
, line
);
262 rr
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_DS
, domain
);
267 rr
->ds
.algorithm
= a
;
268 rr
->ds
.digest_type
= dt
;
269 rr
->ds
.digest_size
= l
;
273 } else if (strcaseeq(type
, "DNSKEY")) {
274 _cleanup_free_
char *flags
= NULL
, *protocol
= NULL
, *algorithm
= NULL
, *key
= NULL
;
275 _cleanup_free_
void *k
= NULL
;
280 r
= extract_many_words(&p
, NULL
, 0, &flags
, &protocol
, &algorithm
, &key
, NULL
);
282 return log_warning_errno(r
, "Failed to parse DNSKEY parameters on line %s:%u: %m", path
, line
);
284 log_warning("Missing DNSKEY parameters on line %s:%u", path
, line
);
288 if (!streq(protocol
, "3")) {
289 log_warning("DNSKEY Protocol is not 3 on line %s:%u", path
, line
);
293 r
= safe_atou16(flags
, &f
);
295 return log_warning_errno(r
, "Failed to parse DNSKEY flags field %s on line %s:%u", flags
, path
, line
);
296 if ((f
& DNSKEY_FLAG_ZONE_KEY
) == 0) {
297 log_warning("DNSKEY lacks zone key bit set on line %s:%u", path
, line
);
300 if ((f
& DNSKEY_FLAG_REVOKE
)) {
301 log_warning("DNSKEY is already revoked on line %s:%u", path
, line
);
305 a
= dnssec_algorithm_from_string(algorithm
);
307 log_warning("Failed to parse DNSKEY algorithm %s on line %s:%u", algorithm
, path
, line
);
311 r
= unbase64mem(key
, strlen(key
), &k
, &l
);
313 return log_warning_errno(r
, "Failed to parse DNSKEY key data %s on line %s:%u", key
, path
, line
);
315 rr
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_DNSKEY
, domain
);
319 rr
->dnskey
.flags
= f
;
320 rr
->dnskey
.protocol
= 3;
321 rr
->dnskey
.algorithm
= a
;
322 rr
->dnskey
.key_size
= l
;
327 log_warning("RR type %s is not supported, ignoring line %s:%u.", type
, path
, line
);
332 log_warning("Trailing garbage on line %s:%u, ignoring line.", path
, line
);
336 r
= hashmap_ensure_allocated(&d
->positive_by_key
, &dns_resource_key_hash_ops
);
340 old_answer
= hashmap_get(d
->positive_by_key
, rr
->key
);
341 answer
= dns_answer_ref(old_answer
);
343 r
= dns_answer_add_extend(&answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
345 return log_error_errno(r
, "Failed to add trust anchor RR: %m");
347 r
= hashmap_replace(d
->positive_by_key
, rr
->key
, answer
);
349 return log_error_errno(r
, "Failed to add answer to trust anchor: %m");
351 old_answer
= dns_answer_unref(old_answer
);
357 static int dns_trust_anchor_load_negative(DnsTrustAnchor
*d
, const char *path
, unsigned line
, const char *s
) {
358 _cleanup_free_
char *domain
= NULL
;
365 r
= extract_first_word(&p
, &domain
, NULL
, EXTRACT_QUOTES
);
367 return log_warning_errno(r
, "Unable to parse line %s:%u: %m", path
, line
);
369 if (!dns_name_is_valid(domain
)) {
370 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain
, path
, line
);
375 log_warning("Trailing garbage at line %s:%u, ignoring line.", path
, line
);
379 r
= set_ensure_allocated(&d
->negative_by_name
, &dns_name_hash_ops
);
383 r
= set_put(d
->negative_by_name
, domain
);
392 static int dns_trust_anchor_load_files(
395 int (*loader
)(DnsTrustAnchor
*d
, const char *path
, unsigned n
, const char *line
)) {
397 _cleanup_strv_free_
char **files
= NULL
;
405 r
= conf_files_list_nulstr(&files
, suffix
, NULL
, trust_anchor_dirs
);
407 return log_error_errno(r
, "Failed to enumerate %s trust anchor files: %m", suffix
);
409 STRV_FOREACH(f
, files
) {
410 _cleanup_fclose_
FILE *g
= NULL
;
419 log_warning_errno(errno
, "Failed to open %s: %m", *f
);
423 FOREACH_LINE(line
, g
, log_warning_errno(errno
, "Failed to read %s, ignoring: %m", *f
)) {
435 (void) loader(d
, *f
, n
, l
);
442 static int domain_name_cmp(const void *a
, const void *b
) {
443 char **x
= (char**) a
, **y
= (char**) b
;
445 return dns_name_compare_func(*x
, *y
);
448 static int dns_trust_anchor_dump(DnsTrustAnchor
*d
) {
454 if (hashmap_isempty(d
->positive_by_key
))
455 log_info("No positive trust anchors defined.");
457 log_info("Positive Trust Anchors:");
458 HASHMAP_FOREACH(a
, d
->positive_by_key
, i
) {
459 DnsResourceRecord
*rr
;
461 DNS_ANSWER_FOREACH(rr
, a
)
462 log_info("%s", dns_resource_record_to_string(rr
));
466 if (set_isempty(d
->negative_by_name
))
467 log_info("No negative trust anchors defined.");
469 _cleanup_free_
char **l
= NULL
, *j
= NULL
;
471 l
= set_get_strv(d
->negative_by_name
);
475 qsort_safe(l
, set_size(d
->negative_by_name
), sizeof(char*), domain_name_cmp
);
477 j
= strv_join(l
, " ");
481 log_info("Negative trust anchors: %s", j
);
487 int dns_trust_anchor_load(DnsTrustAnchor
*d
) {
492 /* If loading things from disk fails, we don't consider this fatal */
493 (void) dns_trust_anchor_load_files(d
, ".positive", dns_trust_anchor_load_positive
);
494 (void) dns_trust_anchor_load_files(d
, ".negative", dns_trust_anchor_load_negative
);
496 /* However, if the built-in DS fails, then we have a problem. */
497 r
= dns_trust_anchor_add_builtin_positive(d
);
499 return log_error_errno(r
, "Failed to add built-in positive trust anchor: %m");
501 r
= dns_trust_anchor_add_builtin_negative(d
);
503 return log_error_errno(r
, "Failed to add built-in negative trust anchor: %m");
505 dns_trust_anchor_dump(d
);
510 void dns_trust_anchor_flush(DnsTrustAnchor
*d
) {
512 DnsResourceRecord
*rr
;
516 while ((a
= hashmap_steal_first(d
->positive_by_key
)))
518 d
->positive_by_key
= hashmap_free(d
->positive_by_key
);
520 while ((rr
= set_steal_first(d
->revoked_by_rr
)))
521 dns_resource_record_unref(rr
);
522 d
->revoked_by_rr
= set_free(d
->revoked_by_rr
);
524 d
->negative_by_name
= set_free_free(d
->negative_by_name
);
527 int dns_trust_anchor_lookup_positive(DnsTrustAnchor
*d
, const DnsResourceKey
*key
, DnsAnswer
**ret
) {
534 /* We only serve DS and DNSKEY RRs. */
535 if (!IN_SET(key
->type
, DNS_TYPE_DS
, DNS_TYPE_DNSKEY
))
538 a
= hashmap_get(d
->positive_by_key
, key
);
542 *ret
= dns_answer_ref(a
);
546 int dns_trust_anchor_lookup_negative(DnsTrustAnchor
*d
, const char *name
) {
550 return set_contains(d
->negative_by_name
, name
);
553 static int dns_trust_anchor_revoked_put(DnsTrustAnchor
*d
, DnsResourceRecord
*rr
) {
558 r
= set_ensure_allocated(&d
->revoked_by_rr
, &dns_resource_record_hash_ops
);
562 r
= set_put(d
->revoked_by_rr
, rr
);
566 dns_resource_record_ref(rr
);
571 static int dns_trust_anchor_remove_revoked(DnsTrustAnchor
*d
, DnsResourceRecord
*rr
) {
572 _cleanup_(dns_answer_unrefp
) DnsAnswer
*new_answer
= NULL
;
573 DnsAnswer
*old_answer
;
576 /* Remember that this is a revoked trust anchor RR */
577 r
= dns_trust_anchor_revoked_put(d
, rr
);
581 /* Remove this from the positive trust anchor */
582 old_answer
= hashmap_get(d
->positive_by_key
, rr
->key
);
586 new_answer
= dns_answer_ref(old_answer
);
588 r
= dns_answer_remove_by_rr(&new_answer
, rr
);
592 /* We found the key! Warn the user */
593 log_struct(LOG_WARNING
,
594 LOG_MESSAGE_ID(SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED
),
595 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
)),
596 "TRUST_ANCHOR=%s", dns_resource_record_to_string(rr
),
599 if (dns_answer_size(new_answer
) <= 0) {
600 assert_se(hashmap_remove(d
->positive_by_key
, rr
->key
) == old_answer
);
601 dns_answer_unref(old_answer
);
605 r
= hashmap_replace(d
->positive_by_key
, new_answer
->items
[0].rr
->key
, new_answer
);
610 dns_answer_unref(old_answer
);
614 static int dns_trust_anchor_check_revoked_one(DnsTrustAnchor
*d
, DnsResourceRecord
*revoked_dnskey
) {
619 assert(revoked_dnskey
);
620 assert(revoked_dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
621 assert(revoked_dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
);
623 a
= hashmap_get(d
->positive_by_key
, revoked_dnskey
->key
);
625 DnsResourceRecord
*anchor
;
627 /* First, look for the precise DNSKEY in our trust anchor database */
629 DNS_ANSWER_FOREACH(anchor
, a
) {
631 if (anchor
->dnskey
.protocol
!= revoked_dnskey
->dnskey
.protocol
)
634 if (anchor
->dnskey
.algorithm
!= revoked_dnskey
->dnskey
.algorithm
)
637 if (anchor
->dnskey
.key_size
!= revoked_dnskey
->dnskey
.key_size
)
640 /* Note that we allow the REVOKE bit to be
641 * different! It will be set in the revoked
642 * key, but unset in our version of it */
643 if (((anchor
->dnskey
.flags
^ revoked_dnskey
->dnskey
.flags
) | DNSKEY_FLAG_REVOKE
) != DNSKEY_FLAG_REVOKE
)
646 if (memcmp(anchor
->dnskey
.key
, revoked_dnskey
->dnskey
.key
, anchor
->dnskey
.key_size
) != 0)
649 dns_trust_anchor_remove_revoked(d
, anchor
);
654 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
)));
656 DnsResourceRecord
*anchor
;
658 /* Second, look for DS RRs matching this DNSKEY in our trust anchor database */
660 DNS_ANSWER_FOREACH(anchor
, a
) {
662 /* We set mask_revoke to true here, since our
663 * DS fingerprint will be the one of the
664 * unrevoked DNSKEY, but the one we got passed
665 * here has the bit set. */
666 r
= dnssec_verify_dnskey_by_ds(revoked_dnskey
, anchor
, true);
672 dns_trust_anchor_remove_revoked(d
, anchor
);
680 int dns_trust_anchor_check_revoked(DnsTrustAnchor
*d
, DnsResourceRecord
*dnskey
, DnsAnswer
*rrs
) {
681 DnsResourceRecord
*rrsig
;
687 /* Looks if "dnskey" is a self-signed RR that has been revoked
688 * and matches one of our trust anchor entries. If so, removes
689 * it from the trust anchor and returns > 0. */
691 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
694 /* Is this DNSKEY revoked? */
695 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
) == 0)
698 /* Could this be interesting to us at all? If not,
699 * there's no point in looking for and verifying a
700 * self-signed RRSIG. */
701 if (!dns_trust_anchor_knows_domain_positive(d
, DNS_RESOURCE_KEY_NAME(dnskey
->key
)))
704 /* Look for a self-signed RRSIG in the other rrs belonging to this DNSKEY */
705 DNS_ANSWER_FOREACH(rrsig
, rrs
) {
708 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
711 r
= dnssec_rrsig_match_dnskey(rrsig
, dnskey
, true);
717 r
= dnssec_verify_rrset(rrs
, dnskey
->key
, rrsig
, dnskey
, USEC_INFINITY
, &result
);
720 if (result
!= DNSSEC_VALIDATED
)
723 /* Bingo! This is a revoked self-signed DNSKEY. Let's
724 * see if this precise one exists in our trust anchor
726 r
= dns_trust_anchor_check_revoked_one(d
, dnskey
);
736 int dns_trust_anchor_is_revoked(DnsTrustAnchor
*d
, DnsResourceRecord
*rr
) {
739 if (!IN_SET(rr
->key
->type
, DNS_TYPE_DS
, DNS_TYPE_DNSKEY
))
742 return set_contains(d
->revoked_by_rr
, rr
);