1:255.13-alt1
[systemd_ALT.git] / src / resolve / resolved-dns-trust-anchor.c
blob8aea5e11a0ee7625aa45d269734a499f235c6d66
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "sd-messages.h"
5 #include "alloc-util.h"
6 #include "conf-files.h"
7 #include "constants.h"
8 #include "dns-domain.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "hexdecoct.h"
12 #include "nulstr-util.h"
13 #include "parse-util.h"
14 #include "resolved-dns-dnssec.h"
15 #include "resolved-dns-trust-anchor.h"
16 #include "set.h"
17 #include "sort-util.h"
18 #include "string-util.h"
19 #include "strv.h"
21 static const char trust_anchor_dirs[] = CONF_PATHS_NULSTR("dnssec-trust-anchors.d");
23 /* The second DS RR from https://data.iana.org/root-anchors/root-anchors.xml, retrieved February 2017 */
24 static const uint8_t root_digest2[] =
25 { 0xE0, 0x6D, 0x44, 0xB8, 0x0B, 0x8F, 0x1D, 0x39, 0xA9, 0x5C, 0x0B, 0x0D, 0x7C, 0x65, 0xD0, 0x84,
26 0x58, 0xE8, 0x80, 0x40, 0x9B, 0xBC, 0x68, 0x34, 0x57, 0x10, 0x42, 0x37, 0xC7, 0xF8, 0xEC, 0x8D };
28 static bool dns_trust_anchor_knows_domain_positive(DnsTrustAnchor *d, const char *name) {
29 assert(d);
31 /* Returns true if there's an entry for the specified domain
32 * name in our trust anchor */
34 return
35 hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DNSKEY, name)) ||
36 hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DS, name));
39 static int add_root_ksk(
40 DnsAnswer *answer,
41 DnsResourceKey *key,
42 uint16_t key_tag,
43 uint8_t algorithm,
44 uint8_t digest_type,
45 const void *digest,
46 size_t digest_size) {
48 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
49 int r;
51 rr = dns_resource_record_new(key);
52 if (!rr)
53 return -ENOMEM;
55 rr->ds.key_tag = key_tag;
56 rr->ds.algorithm = algorithm;
57 rr->ds.digest_type = digest_type;
58 rr->ds.digest_size = digest_size;
59 rr->ds.digest = memdup(digest, rr->ds.digest_size);
60 if (!rr->ds.digest)
61 return -ENOMEM;
63 r = dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL);
64 if (r < 0)
65 return r;
67 return 0;
70 static int dns_trust_anchor_add_builtin_positive(DnsTrustAnchor *d) {
71 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
72 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
73 int r;
75 assert(d);
77 r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
78 if (r < 0)
79 return r;
81 /* Only add the built-in trust anchor if there's neither a DS nor a DNSKEY defined for the root domain. That
82 * way users have an easy way to override the root domain DS/DNSKEY data. */
83 if (dns_trust_anchor_knows_domain_positive(d, "."))
84 return 0;
86 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_DS, "");
87 if (!key)
88 return -ENOMEM;
90 answer = dns_answer_new(2);
91 if (!answer)
92 return -ENOMEM;
94 /* Add the currently valid RRs from https://data.iana.org/root-anchors/root-anchors.xml */
95 r = add_root_ksk(answer, key, 20326, DNSSEC_ALGORITHM_RSASHA256, DNSSEC_DIGEST_SHA256, root_digest2, sizeof(root_digest2));
96 if (r < 0)
97 return r;
99 r = hashmap_put(d->positive_by_key, key, answer);
100 if (r < 0)
101 return r;
103 answer = NULL;
104 return 0;
107 static int dns_trust_anchor_add_builtin_negative(DnsTrustAnchor *d) {
109 static const char private_domains[] =
110 /* RFC 6761 says that .test is a special domain for
111 * testing and not to be installed in the root zone */
112 "test\0"
114 /* RFC 6761 says that these reverse IP lookup ranges
115 * are for private addresses, and hence should not
116 * show up in the root zone */
117 "10.in-addr.arpa\0"
118 "16.172.in-addr.arpa\0"
119 "17.172.in-addr.arpa\0"
120 "18.172.in-addr.arpa\0"
121 "19.172.in-addr.arpa\0"
122 "20.172.in-addr.arpa\0"
123 "21.172.in-addr.arpa\0"
124 "22.172.in-addr.arpa\0"
125 "23.172.in-addr.arpa\0"
126 "24.172.in-addr.arpa\0"
127 "25.172.in-addr.arpa\0"
128 "26.172.in-addr.arpa\0"
129 "27.172.in-addr.arpa\0"
130 "28.172.in-addr.arpa\0"
131 "29.172.in-addr.arpa\0"
132 "30.172.in-addr.arpa\0"
133 "31.172.in-addr.arpa\0"
134 "168.192.in-addr.arpa\0"
136 /* The same, but for IPv6. */
137 "d.f.ip6.arpa\0"
139 /* RFC 6762 reserves the .local domain for Multicast
140 * DNS, it hence cannot appear in the root zone. (Note
141 * that we by default do not route .local traffic to
142 * DNS anyway, except when a configured search domain
143 * suggests so.) */
144 "local\0"
146 /* These two are well known, popular private zone
147 * TLDs, that are blocked from delegation, according
148 * to:
149 * http://icannwiki.com/Name_Collision#NGPC_Resolution
151 * There's also ongoing work on making this official
152 * in an RRC:
153 * https://www.ietf.org/archive/id/draft-chapin-additional-reserved-tlds-02.txt */
154 "home\0"
155 "corp\0"
157 /* The following four TLDs are suggested for private
158 * zones in RFC 6762, Appendix G, and are hence very
159 * unlikely to be made official TLDs any day soon */
160 "lan\0"
161 "intranet\0"
162 "internal\0"
163 "private\0"
165 /* Defined by RFC 8375. The most official choice. */
166 "home.arpa\0"
168 /* RFC 9462 doesn't mention DNSSEC, but this domain
169 * can't really be signed and clients need to validate
170 * the answer before using it anyway. */
171 "resolver.arpa\0"
173 /* RFC 8880 says because the 'ipv4only.arpa' zone has to
174 * be an insecure delegation, DNSSEC cannot be used to
175 * protect these answers from tampering by malicious
176 * devices on the path */
177 "ipv4only.arpa\0"
178 "170.0.0.192.in-addr.arpa\0"
179 "171.0.0.192.in-addr.arpa\0";
181 int r;
183 assert(d);
185 /* Only add the built-in trust anchor if there's no negative
186 * trust anchor defined at all. This enables easy overriding
187 * of negative trust anchors. */
189 if (set_size(d->negative_by_name) > 0)
190 return 0;
192 r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops);
193 if (r < 0)
194 return r;
196 /* We add a couple of domains as default negative trust
197 * anchors, where it's very unlikely they will be installed in
198 * the root zone. If they exist they must be private, and thus
199 * unsigned. */
201 NULSTR_FOREACH(name, private_domains) {
202 if (dns_trust_anchor_knows_domain_positive(d, name))
203 continue;
205 r = set_put_strdup(&d->negative_by_name, name);
206 if (r < 0)
207 return r;
210 return 0;
213 static int dns_trust_anchor_load_positive(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
214 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
215 _cleanup_free_ char *domain = NULL, *class = NULL, *type = NULL;
216 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
217 DnsAnswer *old_answer = NULL;
218 const char *p = s;
219 int r;
221 assert(d);
222 assert(line);
224 r = extract_first_word(&p, &domain, NULL, EXTRACT_UNQUOTE);
225 if (r < 0)
226 return log_warning_errno(r, "Unable to parse domain in line %s:%u: %m", path, line);
228 r = dns_name_is_valid(domain);
229 if (r < 0)
230 return log_warning_errno(r, "Failed to check validity of domain name '%s', at line %s:%u, ignoring line: %m", domain, path, line);
231 if (r == 0) {
232 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
233 return -EINVAL;
236 r = extract_many_words(&p, NULL, 0, &class, &type, NULL);
237 if (r < 0)
238 return log_warning_errno(r, "Unable to parse class and type in line %s:%u: %m", path, line);
239 if (r != 2) {
240 log_warning("Missing class or type in line %s:%u", path, line);
241 return -EINVAL;
244 if (!strcaseeq(class, "IN")) {
245 log_warning("RR class %s is not supported, ignoring line %s:%u.", class, path, line);
246 return -EINVAL;
249 if (strcaseeq(type, "DS")) {
250 _cleanup_free_ char *key_tag = NULL, *algorithm = NULL, *digest_type = NULL;
251 _cleanup_free_ void *dd = NULL;
252 uint16_t kt;
253 int a, dt;
254 size_t l;
256 r = extract_many_words(&p, NULL, 0, &key_tag, &algorithm, &digest_type, NULL);
257 if (r < 0) {
258 log_warning_errno(r, "Failed to parse DS parameters on line %s:%u: %m", path, line);
259 return -EINVAL;
261 if (r != 3) {
262 log_warning("Missing DS parameters on line %s:%u", path, line);
263 return -EINVAL;
266 r = safe_atou16(key_tag, &kt);
267 if (r < 0)
268 return log_warning_errno(r, "Failed to parse DS key tag %s on line %s:%u: %m", key_tag, path, line);
270 a = dnssec_algorithm_from_string(algorithm);
271 if (a < 0) {
272 log_warning("Failed to parse DS algorithm %s on line %s:%u", algorithm, path, line);
273 return -EINVAL;
276 dt = dnssec_digest_from_string(digest_type);
277 if (dt < 0) {
278 log_warning("Failed to parse DS digest type %s on line %s:%u", digest_type, path, line);
279 return -EINVAL;
282 if (isempty(p)) {
283 log_warning("Missing DS digest on line %s:%u", path, line);
284 return -EINVAL;
287 r = unhexmem(p, strlen(p), &dd, &l);
288 if (r < 0) {
289 log_warning("Failed to parse DS digest %s on line %s:%u", p, path, line);
290 return -EINVAL;
293 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, domain);
294 if (!rr)
295 return log_oom();
297 rr->ds.key_tag = kt;
298 rr->ds.algorithm = a;
299 rr->ds.digest_type = dt;
300 rr->ds.digest_size = l;
301 rr->ds.digest = TAKE_PTR(dd);
303 } else if (strcaseeq(type, "DNSKEY")) {
304 _cleanup_free_ char *flags = NULL, *protocol = NULL, *algorithm = NULL;
305 _cleanup_free_ void *k = NULL;
306 uint16_t f;
307 size_t l;
308 int a;
310 r = extract_many_words(&p, NULL, 0, &flags, &protocol, &algorithm, NULL);
311 if (r < 0)
312 return log_warning_errno(r, "Failed to parse DNSKEY parameters on line %s:%u: %m", path, line);
313 if (r != 3) {
314 log_warning("Missing DNSKEY parameters on line %s:%u", path, line);
315 return -EINVAL;
318 if (!streq(protocol, "3")) {
319 log_warning("DNSKEY Protocol is not 3 on line %s:%u", path, line);
320 return -EINVAL;
323 r = safe_atou16(flags, &f);
324 if (r < 0)
325 return log_warning_errno(r, "Failed to parse DNSKEY flags field %s on line %s:%u", flags, path, line);
326 if ((f & DNSKEY_FLAG_ZONE_KEY) == 0) {
327 log_warning("DNSKEY lacks zone key bit set on line %s:%u", path, line);
328 return -EINVAL;
330 if ((f & DNSKEY_FLAG_REVOKE)) {
331 log_warning("DNSKEY is already revoked on line %s:%u", path, line);
332 return -EINVAL;
335 a = dnssec_algorithm_from_string(algorithm);
336 if (a < 0) {
337 log_warning("Failed to parse DNSKEY algorithm %s on line %s:%u", algorithm, path, line);
338 return -EINVAL;
341 if (isempty(p)) {
342 log_warning("Missing DNSKEY key on line %s:%u", path, line);
343 return -EINVAL;
346 r = unbase64mem(p, strlen(p), &k, &l);
347 if (r < 0)
348 return log_warning_errno(r, "Failed to parse DNSKEY key data %s on line %s:%u", p, path, line);
350 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, domain);
351 if (!rr)
352 return log_oom();
354 rr->dnskey.flags = f;
355 rr->dnskey.protocol = 3;
356 rr->dnskey.algorithm = a;
357 rr->dnskey.key_size = l;
358 rr->dnskey.key = TAKE_PTR(k);
360 } else {
361 log_warning("RR type %s is not supported, ignoring line %s:%u.", type, path, line);
362 return -EINVAL;
365 r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
366 if (r < 0)
367 return log_oom();
369 old_answer = hashmap_get(d->positive_by_key, rr->key);
370 answer = dns_answer_ref(old_answer);
372 r = dns_answer_add_extend(&answer, rr, 0, DNS_ANSWER_AUTHENTICATED, NULL);
373 if (r < 0)
374 return log_error_errno(r, "Failed to add trust anchor RR: %m");
376 r = hashmap_replace(d->positive_by_key, rr->key, answer);
377 if (r < 0)
378 return log_error_errno(r, "Failed to add answer to trust anchor: %m");
380 old_answer = dns_answer_unref(old_answer);
381 answer = NULL;
383 return 0;
386 static int dns_trust_anchor_load_negative(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
387 _cleanup_free_ char *domain = NULL;
388 const char *p = s;
389 int r;
391 assert(d);
392 assert(line);
394 r = extract_first_word(&p, &domain, NULL, EXTRACT_UNQUOTE);
395 if (r < 0)
396 return log_warning_errno(r, "Unable to parse line %s:%u: %m", path, line);
398 r = dns_name_is_valid(domain);
399 if (r < 0)
400 return log_warning_errno(r, "Failed to check validity of domain name '%s', at line %s:%u, ignoring line: %m", domain, path, line);
401 if (r == 0) {
402 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
403 return -EINVAL;
406 if (!isempty(p)) {
407 log_warning("Trailing garbage at line %s:%u, ignoring line.", path, line);
408 return -EINVAL;
411 r = set_ensure_consume(&d->negative_by_name, &dns_name_hash_ops, TAKE_PTR(domain));
412 if (r < 0)
413 return log_oom();
415 return 0;
418 static int dns_trust_anchor_load_files(
419 DnsTrustAnchor *d,
420 const char *suffix,
421 int (*loader)(DnsTrustAnchor *d, const char *path, unsigned n, const char *line)) {
423 _cleanup_strv_free_ char **files = NULL;
424 int r;
426 assert(d);
427 assert(suffix);
428 assert(loader);
430 r = conf_files_list_nulstr(&files, suffix, NULL, 0, trust_anchor_dirs);
431 if (r < 0)
432 return log_error_errno(r, "Failed to enumerate %s trust anchor files: %m", suffix);
434 STRV_FOREACH(f, files) {
435 _cleanup_fclose_ FILE *g = NULL;
436 unsigned n = 0;
438 g = fopen(*f, "re");
439 if (!g) {
440 if (errno == ENOENT)
441 continue;
443 log_warning_errno(errno, "Failed to open '%s', ignoring: %m", *f);
444 continue;
447 for (;;) {
448 _cleanup_free_ char *line = NULL;
450 r = read_stripped_line(g, LONG_LINE_MAX, &line);
451 if (r < 0) {
452 log_warning_errno(r, "Failed to read '%s', ignoring: %m", *f);
453 break;
455 if (r == 0)
456 break;
458 n++;
460 if (isempty(line))
461 continue;
463 if (*line == ';')
464 continue;
466 (void) loader(d, *f, n, line);
470 return 0;
473 static int domain_name_cmp(char * const *a, char * const *b) {
474 return dns_name_compare_func(*a, *b);
477 static int dns_trust_anchor_dump(DnsTrustAnchor *d) {
478 DnsAnswer *a;
480 assert(d);
482 if (hashmap_isempty(d->positive_by_key))
483 log_info("No positive trust anchors defined.");
484 else {
485 log_info("Positive Trust Anchors:");
486 HASHMAP_FOREACH(a, d->positive_by_key) {
487 DnsResourceRecord *rr;
489 DNS_ANSWER_FOREACH(rr, a)
490 log_info("%s", dns_resource_record_to_string(rr));
494 if (set_isempty(d->negative_by_name))
495 log_info("No negative trust anchors defined.");
496 else {
497 _cleanup_free_ char **l = NULL, *j = NULL;
499 l = set_get_strv(d->negative_by_name);
500 if (!l)
501 return log_oom();
503 typesafe_qsort(l, set_size(d->negative_by_name), domain_name_cmp);
505 j = strv_join(l, " ");
506 if (!j)
507 return log_oom();
509 log_info("Negative trust anchors: %s", j);
512 return 0;
515 int dns_trust_anchor_load(DnsTrustAnchor *d) {
516 int r;
518 assert(d);
520 /* If loading things from disk fails, we don't consider this fatal */
521 (void) dns_trust_anchor_load_files(d, ".positive", dns_trust_anchor_load_positive);
522 (void) dns_trust_anchor_load_files(d, ".negative", dns_trust_anchor_load_negative);
524 /* However, if the built-in DS fails, then we have a problem. */
525 r = dns_trust_anchor_add_builtin_positive(d);
526 if (r < 0)
527 return log_error_errno(r, "Failed to add built-in positive trust anchor: %m");
529 r = dns_trust_anchor_add_builtin_negative(d);
530 if (r < 0)
531 return log_error_errno(r, "Failed to add built-in negative trust anchor: %m");
533 dns_trust_anchor_dump(d);
535 return 0;
538 void dns_trust_anchor_flush(DnsTrustAnchor *d) {
539 assert(d);
541 d->positive_by_key = hashmap_free_with_destructor(d->positive_by_key, dns_answer_unref);
542 d->revoked_by_rr = set_free_with_destructor(d->revoked_by_rr, dns_resource_record_unref);
543 d->negative_by_name = set_free_free(d->negative_by_name);
546 int dns_trust_anchor_lookup_positive(DnsTrustAnchor *d, const DnsResourceKey *key, DnsAnswer **ret) {
547 DnsAnswer *a;
549 assert(d);
550 assert(key);
551 assert(ret);
553 /* We only serve DS and DNSKEY RRs. */
554 if (!IN_SET(key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
555 return 0;
557 a = hashmap_get(d->positive_by_key, key);
558 if (!a)
559 return 0;
561 *ret = dns_answer_ref(a);
562 return 1;
565 int dns_trust_anchor_lookup_negative(DnsTrustAnchor *d, const char *name) {
566 int r;
568 assert(d);
569 assert(name);
571 for (;;) {
572 /* If the domain is listed as-is in the NTA database, then that counts */
573 if (set_contains(d->negative_by_name, name))
574 return true;
576 /* If the domain isn't listed as NTA, but is listed as positive trust anchor, then that counts. See RFC
577 * 7646, section 1.1 */
578 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DS, name)))
579 return false;
581 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_KEY, name)))
582 return false;
584 /* And now, let's look at the parent, and check that too */
585 r = dns_name_parent(&name);
586 if (r < 0)
587 return r;
588 if (r == 0)
589 break;
592 return false;
595 static int dns_trust_anchor_revoked_put(DnsTrustAnchor *d, DnsResourceRecord *rr) {
596 int r;
598 assert(d);
600 r = set_ensure_put(&d->revoked_by_rr, &dns_resource_record_hash_ops, rr);
601 if (r < 0)
602 return r;
603 if (r > 0)
604 dns_resource_record_ref(rr);
606 return r;
609 static int dns_trust_anchor_remove_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) {
610 _cleanup_(dns_answer_unrefp) DnsAnswer *new_answer = NULL;
611 DnsAnswer *old_answer;
612 DnsAnswerItem *item;
613 int r;
615 /* Remember that this is a revoked trust anchor RR */
616 r = dns_trust_anchor_revoked_put(d, rr);
617 if (r < 0)
618 return r;
620 /* Remove this from the positive trust anchor */
621 old_answer = hashmap_get(d->positive_by_key, rr->key);
622 if (!old_answer)
623 return 0;
625 new_answer = dns_answer_ref(old_answer);
627 r = dns_answer_remove_by_rr(&new_answer, rr);
628 if (r <= 0)
629 return r;
631 /* We found the key! Warn the user */
632 log_struct(LOG_WARNING,
633 "MESSAGE_ID=" SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED_STR,
634 LOG_MESSAGE("DNSSEC trust anchor %s has been revoked.\n"
635 "Please update the trust anchor, or upgrade your operating system.",
636 strna(dns_resource_record_to_string(rr))),
637 "TRUST_ANCHOR=%s", dns_resource_record_to_string(rr));
639 if (dns_answer_size(new_answer) <= 0) {
640 assert_se(hashmap_remove(d->positive_by_key, rr->key) == old_answer);
641 dns_answer_unref(old_answer);
642 return 1;
645 item = ordered_set_first(new_answer->items);
646 r = hashmap_replace(d->positive_by_key, item->rr->key, new_answer);
647 if (r < 0)
648 return r;
650 TAKE_PTR(new_answer);
651 dns_answer_unref(old_answer);
652 return 1;
655 static int dns_trust_anchor_check_revoked_one(DnsTrustAnchor *d, DnsResourceRecord *revoked_dnskey) {
656 DnsAnswer *a;
657 int r;
659 assert(d);
660 assert(revoked_dnskey);
661 assert(revoked_dnskey->key->type == DNS_TYPE_DNSKEY);
662 assert(revoked_dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE);
664 a = hashmap_get(d->positive_by_key, revoked_dnskey->key);
665 if (a) {
666 DnsResourceRecord *anchor;
668 /* First, look for the precise DNSKEY in our trust anchor database */
670 DNS_ANSWER_FOREACH(anchor, a) {
672 if (anchor->dnskey.protocol != revoked_dnskey->dnskey.protocol)
673 continue;
675 if (anchor->dnskey.algorithm != revoked_dnskey->dnskey.algorithm)
676 continue;
678 if (anchor->dnskey.key_size != revoked_dnskey->dnskey.key_size)
679 continue;
681 /* Note that we allow the REVOKE bit to be
682 * different! It will be set in the revoked
683 * key, but unset in our version of it */
684 if (((anchor->dnskey.flags ^ revoked_dnskey->dnskey.flags) | DNSKEY_FLAG_REVOKE) != DNSKEY_FLAG_REVOKE)
685 continue;
687 if (memcmp(anchor->dnskey.key, revoked_dnskey->dnskey.key, anchor->dnskey.key_size) != 0)
688 continue;
690 dns_trust_anchor_remove_revoked(d, anchor);
691 break;
695 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)));
696 if (a) {
697 DnsResourceRecord *anchor;
699 /* Second, look for DS RRs matching this DNSKEY in our trust anchor database */
701 DNS_ANSWER_FOREACH(anchor, a) {
703 /* We set mask_revoke to true here, since our
704 * DS fingerprint will be the one of the
705 * unrevoked DNSKEY, but the one we got passed
706 * here has the bit set. */
707 r = dnssec_verify_dnskey_by_ds(revoked_dnskey, anchor, true);
708 if (r < 0)
709 return r;
710 if (r == 0)
711 continue;
713 dns_trust_anchor_remove_revoked(d, anchor);
714 break;
718 return 0;
721 int dns_trust_anchor_check_revoked(DnsTrustAnchor *d, DnsResourceRecord *dnskey, DnsAnswer *rrs) {
722 DnsResourceRecord *rrsig;
723 int r;
725 assert(d);
726 assert(dnskey);
728 /* Looks if "dnskey" is a self-signed RR that has been revoked
729 * and matches one of our trust anchor entries. If so, removes
730 * it from the trust anchor and returns > 0. */
732 if (dnskey->key->type != DNS_TYPE_DNSKEY)
733 return 0;
735 /* Is this DNSKEY revoked? */
736 if ((dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE) == 0)
737 return 0;
739 /* Could this be interesting to us at all? If not,
740 * there's no point in looking for and verifying a
741 * self-signed RRSIG. */
742 if (!dns_trust_anchor_knows_domain_positive(d, dns_resource_key_name(dnskey->key)))
743 return 0;
745 /* Look for a self-signed RRSIG in the other rrs belonging to this DNSKEY */
746 DNS_ANSWER_FOREACH(rrsig, rrs) {
747 DnssecResult result;
749 if (rrsig->key->type != DNS_TYPE_RRSIG)
750 continue;
752 r = dnssec_rrsig_match_dnskey(rrsig, dnskey, true);
753 if (r < 0)
754 return r;
755 if (r == 0)
756 continue;
758 r = dnssec_verify_rrset(rrs, dnskey->key, rrsig, dnskey, USEC_INFINITY, &result);
759 if (r < 0)
760 return r;
761 if (result != DNSSEC_VALIDATED)
762 continue;
764 /* Bingo! This is a revoked self-signed DNSKEY. Let's
765 * see if this precise one exists in our trust anchor
766 * database, too. */
767 r = dns_trust_anchor_check_revoked_one(d, dnskey);
768 if (r < 0)
769 return r;
771 return 1;
774 return 0;
777 int dns_trust_anchor_is_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) {
778 assert(d);
780 if (!IN_SET(rr->key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
781 return 0;
783 return set_contains(d->revoked_by_rr, rr);