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 /* The same, but for IPv6. */
133 /* RFC 6762 reserves the .local domain for Multicast
134 * DNS, it hence cannot appear in the root zone. (Note
135 * that we by default do not route .local traffic to
136 * DNS anyway, except when a configured search domain
140 /* These two are well known, popular private zone
141 * TLDs, that are blocked from delegation, according
143 * http://icannwiki.com/Name_Collision#NGPC_Resolution
145 * There's also ongoing work on making this official
147 * https://www.ietf.org/archive/id/draft-chapin-additional-reserved-tlds-02.txt */
151 /* The following four TLDs are suggested for private
152 * zones in RFC 6762, Appendix G, and are hence very
153 * unlikely to be made official TLDs any day soon */
164 /* Only add the built-in trust anchor if there's no negative
165 * trust anchor defined at all. This enables easy overriding
166 * of negative trust anchors. */
168 if (set_size(d
->negative_by_name
) > 0)
171 r
= set_ensure_allocated(&d
->negative_by_name
, &dns_name_hash_ops
);
175 /* We add a couple of domains as default negative trust
176 * anchors, where it's very unlikely they will be installed in
177 * the root zone. If they exist they must be private, and thus
180 NULSTR_FOREACH(name
, private_domains
) {
182 if (dns_trust_anchor_knows_domain_positive(d
, name
))
185 r
= set_put_strdup(d
->negative_by_name
, name
);
193 static int dns_trust_anchor_load_positive(DnsTrustAnchor
*d
, const char *path
, unsigned line
, const char *s
) {
194 _cleanup_(dns_resource_record_unrefp
) DnsResourceRecord
*rr
= NULL
;
195 _cleanup_free_
char *domain
= NULL
, *class = NULL
, *type
= NULL
;
196 _cleanup_(dns_answer_unrefp
) DnsAnswer
*answer
= NULL
;
197 DnsAnswer
*old_answer
= NULL
;
204 r
= extract_first_word(&p
, &domain
, NULL
, EXTRACT_QUOTES
);
206 return log_warning_errno(r
, "Unable to parse domain in line %s:%u: %m", path
, line
);
208 if (!dns_name_is_valid(domain
)) {
209 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain
, path
, line
);
213 r
= extract_many_words(&p
, NULL
, 0, &class, &type
, NULL
);
215 return log_warning_errno(r
, "Unable to parse class and type in line %s:%u: %m", path
, line
);
217 log_warning("Missing class or type in line %s:%u", path
, line
);
221 if (!strcaseeq(class, "IN")) {
222 log_warning("RR class %s is not supported, ignoring line %s:%u.", class, path
, line
);
226 if (strcaseeq(type
, "DS")) {
227 _cleanup_free_
char *key_tag
= NULL
, *algorithm
= NULL
, *digest_type
= NULL
, *digest
= NULL
;
228 _cleanup_free_
void *dd
= NULL
;
233 r
= extract_many_words(&p
, NULL
, 0, &key_tag
, &algorithm
, &digest_type
, &digest
, NULL
);
235 log_warning_errno(r
, "Failed to parse DS parameters on line %s:%u: %m", path
, line
);
239 log_warning("Missing DS parameters on line %s:%u", path
, line
);
243 r
= safe_atou16(key_tag
, &kt
);
245 return log_warning_errno(r
, "Failed to parse DS key tag %s on line %s:%u: %m", key_tag
, path
, line
);
247 a
= dnssec_algorithm_from_string(algorithm
);
249 log_warning("Failed to parse DS algorithm %s on line %s:%u", algorithm
, path
, line
);
253 dt
= dnssec_digest_from_string(digest_type
);
255 log_warning("Failed to parse DS digest type %s on line %s:%u", digest_type
, path
, line
);
259 r
= unhexmem(digest
, strlen(digest
), &dd
, &l
);
261 log_warning("Failed to parse DS digest %s on line %s:%u", digest
, path
, line
);
265 rr
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_DS
, domain
);
270 rr
->ds
.algorithm
= a
;
271 rr
->ds
.digest_type
= dt
;
272 rr
->ds
.digest_size
= l
;
276 } else if (strcaseeq(type
, "DNSKEY")) {
277 _cleanup_free_
char *flags
= NULL
, *protocol
= NULL
, *algorithm
= NULL
, *key
= NULL
;
278 _cleanup_free_
void *k
= NULL
;
283 r
= extract_many_words(&p
, NULL
, 0, &flags
, &protocol
, &algorithm
, &key
, NULL
);
285 return log_warning_errno(r
, "Failed to parse DNSKEY parameters on line %s:%u: %m", path
, line
);
287 log_warning("Missing DNSKEY parameters on line %s:%u", path
, line
);
291 if (!streq(protocol
, "3")) {
292 log_warning("DNSKEY Protocol is not 3 on line %s:%u", path
, line
);
296 r
= safe_atou16(flags
, &f
);
298 return log_warning_errno(r
, "Failed to parse DNSKEY flags field %s on line %s:%u", flags
, path
, line
);
299 if ((f
& DNSKEY_FLAG_ZONE_KEY
) == 0) {
300 log_warning("DNSKEY lacks zone key bit set on line %s:%u", path
, line
);
303 if ((f
& DNSKEY_FLAG_REVOKE
)) {
304 log_warning("DNSKEY is already revoked on line %s:%u", path
, line
);
308 a
= dnssec_algorithm_from_string(algorithm
);
310 log_warning("Failed to parse DNSKEY algorithm %s on line %s:%u", algorithm
, path
, line
);
314 r
= unbase64mem(key
, strlen(key
), &k
, &l
);
316 return log_warning_errno(r
, "Failed to parse DNSKEY key data %s on line %s:%u", key
, path
, line
);
318 rr
= dns_resource_record_new_full(DNS_CLASS_IN
, DNS_TYPE_DNSKEY
, domain
);
322 rr
->dnskey
.flags
= f
;
323 rr
->dnskey
.protocol
= 3;
324 rr
->dnskey
.algorithm
= a
;
325 rr
->dnskey
.key_size
= l
;
330 log_warning("RR type %s is not supported, ignoring line %s:%u.", type
, path
, line
);
335 log_warning("Trailing garbage on line %s:%u, ignoring line.", path
, line
);
339 r
= hashmap_ensure_allocated(&d
->positive_by_key
, &dns_resource_key_hash_ops
);
343 old_answer
= hashmap_get(d
->positive_by_key
, rr
->key
);
344 answer
= dns_answer_ref(old_answer
);
346 r
= dns_answer_add_extend(&answer
, rr
, 0, DNS_ANSWER_AUTHENTICATED
);
348 return log_error_errno(r
, "Failed to add trust anchor RR: %m");
350 r
= hashmap_replace(d
->positive_by_key
, rr
->key
, answer
);
352 return log_error_errno(r
, "Failed to add answer to trust anchor: %m");
354 old_answer
= dns_answer_unref(old_answer
);
360 static int dns_trust_anchor_load_negative(DnsTrustAnchor
*d
, const char *path
, unsigned line
, const char *s
) {
361 _cleanup_free_
char *domain
= NULL
;
368 r
= extract_first_word(&p
, &domain
, NULL
, EXTRACT_QUOTES
);
370 return log_warning_errno(r
, "Unable to parse line %s:%u: %m", path
, line
);
372 if (!dns_name_is_valid(domain
)) {
373 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain
, path
, line
);
378 log_warning("Trailing garbage at line %s:%u, ignoring line.", path
, line
);
382 r
= set_ensure_allocated(&d
->negative_by_name
, &dns_name_hash_ops
);
386 r
= set_put(d
->negative_by_name
, domain
);
395 static int dns_trust_anchor_load_files(
398 int (*loader
)(DnsTrustAnchor
*d
, const char *path
, unsigned n
, const char *line
)) {
400 _cleanup_strv_free_
char **files
= NULL
;
408 r
= conf_files_list_nulstr(&files
, suffix
, NULL
, trust_anchor_dirs
);
410 return log_error_errno(r
, "Failed to enumerate %s trust anchor files: %m", suffix
);
412 STRV_FOREACH(f
, files
) {
413 _cleanup_fclose_
FILE *g
= NULL
;
422 log_warning_errno(errno
, "Failed to open %s: %m", *f
);
426 FOREACH_LINE(line
, g
, log_warning_errno(errno
, "Failed to read %s, ignoring: %m", *f
)) {
438 (void) loader(d
, *f
, n
, l
);
445 static int domain_name_cmp(const void *a
, const void *b
) {
446 char **x
= (char**) a
, **y
= (char**) b
;
448 return dns_name_compare_func(*x
, *y
);
451 static int dns_trust_anchor_dump(DnsTrustAnchor
*d
) {
457 if (hashmap_isempty(d
->positive_by_key
))
458 log_info("No positive trust anchors defined.");
460 log_info("Positive Trust Anchors:");
461 HASHMAP_FOREACH(a
, d
->positive_by_key
, i
) {
462 DnsResourceRecord
*rr
;
464 DNS_ANSWER_FOREACH(rr
, a
)
465 log_info("%s", dns_resource_record_to_string(rr
));
469 if (set_isempty(d
->negative_by_name
))
470 log_info("No negative trust anchors defined.");
472 _cleanup_free_
char **l
= NULL
, *j
= NULL
;
474 l
= set_get_strv(d
->negative_by_name
);
478 qsort_safe(l
, set_size(d
->negative_by_name
), sizeof(char*), domain_name_cmp
);
480 j
= strv_join(l
, " ");
484 log_info("Negative trust anchors: %s", j
);
490 int dns_trust_anchor_load(DnsTrustAnchor
*d
) {
495 /* If loading things from disk fails, we don't consider this fatal */
496 (void) dns_trust_anchor_load_files(d
, ".positive", dns_trust_anchor_load_positive
);
497 (void) dns_trust_anchor_load_files(d
, ".negative", dns_trust_anchor_load_negative
);
499 /* However, if the built-in DS fails, then we have a problem. */
500 r
= dns_trust_anchor_add_builtin_positive(d
);
502 return log_error_errno(r
, "Failed to add built-in positive trust anchor: %m");
504 r
= dns_trust_anchor_add_builtin_negative(d
);
506 return log_error_errno(r
, "Failed to add built-in negative trust anchor: %m");
508 dns_trust_anchor_dump(d
);
513 void dns_trust_anchor_flush(DnsTrustAnchor
*d
) {
515 DnsResourceRecord
*rr
;
519 while ((a
= hashmap_steal_first(d
->positive_by_key
)))
521 d
->positive_by_key
= hashmap_free(d
->positive_by_key
);
523 while ((rr
= set_steal_first(d
->revoked_by_rr
)))
524 dns_resource_record_unref(rr
);
525 d
->revoked_by_rr
= set_free(d
->revoked_by_rr
);
527 d
->negative_by_name
= set_free_free(d
->negative_by_name
);
530 int dns_trust_anchor_lookup_positive(DnsTrustAnchor
*d
, const DnsResourceKey
*key
, DnsAnswer
**ret
) {
537 /* We only serve DS and DNSKEY RRs. */
538 if (!IN_SET(key
->type
, DNS_TYPE_DS
, DNS_TYPE_DNSKEY
))
541 a
= hashmap_get(d
->positive_by_key
, key
);
545 *ret
= dns_answer_ref(a
);
549 int dns_trust_anchor_lookup_negative(DnsTrustAnchor
*d
, const char *name
) {
553 return set_contains(d
->negative_by_name
, name
);
556 static int dns_trust_anchor_revoked_put(DnsTrustAnchor
*d
, DnsResourceRecord
*rr
) {
561 r
= set_ensure_allocated(&d
->revoked_by_rr
, &dns_resource_record_hash_ops
);
565 r
= set_put(d
->revoked_by_rr
, rr
);
569 dns_resource_record_ref(rr
);
574 static int dns_trust_anchor_remove_revoked(DnsTrustAnchor
*d
, DnsResourceRecord
*rr
) {
575 _cleanup_(dns_answer_unrefp
) DnsAnswer
*new_answer
= NULL
;
576 DnsAnswer
*old_answer
;
579 /* Remember that this is a revoked trust anchor RR */
580 r
= dns_trust_anchor_revoked_put(d
, rr
);
584 /* Remove this from the positive trust anchor */
585 old_answer
= hashmap_get(d
->positive_by_key
, rr
->key
);
589 new_answer
= dns_answer_ref(old_answer
);
591 r
= dns_answer_remove_by_rr(&new_answer
, rr
);
595 /* We found the key! Warn the user */
596 log_struct(LOG_WARNING
,
597 "MESSAGE_ID=" SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED_STR
,
598 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
)),
599 "TRUST_ANCHOR=%s", dns_resource_record_to_string(rr
),
602 if (dns_answer_size(new_answer
) <= 0) {
603 assert_se(hashmap_remove(d
->positive_by_key
, rr
->key
) == old_answer
);
604 dns_answer_unref(old_answer
);
608 r
= hashmap_replace(d
->positive_by_key
, new_answer
->items
[0].rr
->key
, new_answer
);
613 dns_answer_unref(old_answer
);
617 static int dns_trust_anchor_check_revoked_one(DnsTrustAnchor
*d
, DnsResourceRecord
*revoked_dnskey
) {
622 assert(revoked_dnskey
);
623 assert(revoked_dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
624 assert(revoked_dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
);
626 a
= hashmap_get(d
->positive_by_key
, revoked_dnskey
->key
);
628 DnsResourceRecord
*anchor
;
630 /* First, look for the precise DNSKEY in our trust anchor database */
632 DNS_ANSWER_FOREACH(anchor
, a
) {
634 if (anchor
->dnskey
.protocol
!= revoked_dnskey
->dnskey
.protocol
)
637 if (anchor
->dnskey
.algorithm
!= revoked_dnskey
->dnskey
.algorithm
)
640 if (anchor
->dnskey
.key_size
!= revoked_dnskey
->dnskey
.key_size
)
643 /* Note that we allow the REVOKE bit to be
644 * different! It will be set in the revoked
645 * key, but unset in our version of it */
646 if (((anchor
->dnskey
.flags
^ revoked_dnskey
->dnskey
.flags
) | DNSKEY_FLAG_REVOKE
) != DNSKEY_FLAG_REVOKE
)
649 if (memcmp(anchor
->dnskey
.key
, revoked_dnskey
->dnskey
.key
, anchor
->dnskey
.key_size
) != 0)
652 dns_trust_anchor_remove_revoked(d
, anchor
);
657 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
)));
659 DnsResourceRecord
*anchor
;
661 /* Second, look for DS RRs matching this DNSKEY in our trust anchor database */
663 DNS_ANSWER_FOREACH(anchor
, a
) {
665 /* We set mask_revoke to true here, since our
666 * DS fingerprint will be the one of the
667 * unrevoked DNSKEY, but the one we got passed
668 * here has the bit set. */
669 r
= dnssec_verify_dnskey_by_ds(revoked_dnskey
, anchor
, true);
675 dns_trust_anchor_remove_revoked(d
, anchor
);
683 int dns_trust_anchor_check_revoked(DnsTrustAnchor
*d
, DnsResourceRecord
*dnskey
, DnsAnswer
*rrs
) {
684 DnsResourceRecord
*rrsig
;
690 /* Looks if "dnskey" is a self-signed RR that has been revoked
691 * and matches one of our trust anchor entries. If so, removes
692 * it from the trust anchor and returns > 0. */
694 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
697 /* Is this DNSKEY revoked? */
698 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
) == 0)
701 /* Could this be interesting to us at all? If not,
702 * there's no point in looking for and verifying a
703 * self-signed RRSIG. */
704 if (!dns_trust_anchor_knows_domain_positive(d
, dns_resource_key_name(dnskey
->key
)))
707 /* Look for a self-signed RRSIG in the other rrs belonging to this DNSKEY */
708 DNS_ANSWER_FOREACH(rrsig
, rrs
) {
711 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
714 r
= dnssec_rrsig_match_dnskey(rrsig
, dnskey
, true);
720 r
= dnssec_verify_rrset(rrs
, dnskey
->key
, rrsig
, dnskey
, USEC_INFINITY
, &result
);
723 if (result
!= DNSSEC_VALIDATED
)
726 /* Bingo! This is a revoked self-signed DNSKEY. Let's
727 * see if this precise one exists in our trust anchor
729 r
= dns_trust_anchor_check_revoked_one(d
, dnskey
);
739 int dns_trust_anchor_is_revoked(DnsTrustAnchor
*d
, DnsResourceRecord
*rr
) {
742 if (!IN_SET(rr
->key
->type
, DNS_TYPE_DS
, DNS_TYPE_DNSKEY
))
745 return set_contains(d
->revoked_by_rr
, rr
);