]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-trust-anchor.c
add ipv6 range element creation test cases
[thirdparty/systemd.git] / src / resolve / resolved-dns-trust-anchor.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-messages.h"
4
5 #include "alloc-util.h"
6 #include "conf-files.h"
7 #include "def.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"
20
21 static const char trust_anchor_dirs[] = CONF_PATHS_NULSTR("dnssec-trust-anchors.d");
22
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 };
27
28 static bool dns_trust_anchor_knows_domain_positive(DnsTrustAnchor *d, const char *name) {
29 assert(d);
30
31 /* Returns true if there's an entry for the specified domain
32 * name in our trust anchor */
33
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));
37 }
38
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) {
47
48 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
49 int r;
50
51 rr = dns_resource_record_new(key);
52 if (!rr)
53 return -ENOMEM;
54
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;
62
63 r = dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
64 if (r < 0)
65 return r;
66
67 return 0;
68 }
69
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;
74
75 assert(d);
76
77 r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
78 if (r < 0)
79 return r;
80
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;
85
86 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_DS, "");
87 if (!key)
88 return -ENOMEM;
89
90 answer = dns_answer_new(2);
91 if (!answer)
92 return -ENOMEM;
93
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;
98
99 r = hashmap_put(d->positive_by_key, key, answer);
100 if (r < 0)
101 return r;
102
103 answer = NULL;
104 return 0;
105 }
106
107 static int dns_trust_anchor_add_builtin_negative(DnsTrustAnchor *d) {
108
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"
113
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"
135
136 /* The same, but for IPv6. */
137 "d.f.ip6.arpa\0"
138
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"
145
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
150 *
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"
156
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";
164
165 const char *name;
166 int r;
167
168 assert(d);
169
170 /* Only add the built-in trust anchor if there's no negative
171 * trust anchor defined at all. This enables easy overriding
172 * of negative trust anchors. */
173
174 if (set_size(d->negative_by_name) > 0)
175 return 0;
176
177 r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops);
178 if (r < 0)
179 return r;
180
181 /* We add a couple of domains as default negative trust
182 * anchors, where it's very unlikely they will be installed in
183 * the root zone. If they exist they must be private, and thus
184 * unsigned. */
185
186 NULSTR_FOREACH(name, private_domains) {
187 if (dns_trust_anchor_knows_domain_positive(d, name))
188 continue;
189
190 r = set_put_strdup(&d->negative_by_name, name);
191 if (r < 0)
192 return r;
193 }
194
195 return 0;
196 }
197
198 static int dns_trust_anchor_load_positive(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
199 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
200 _cleanup_free_ char *domain = NULL, *class = NULL, *type = NULL;
201 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
202 DnsAnswer *old_answer = NULL;
203 const char *p = s;
204 int r;
205
206 assert(d);
207 assert(line);
208
209 r = extract_first_word(&p, &domain, NULL, EXTRACT_UNQUOTE);
210 if (r < 0)
211 return log_warning_errno(r, "Unable to parse domain in line %s:%u: %m", path, line);
212
213 r = dns_name_is_valid(domain);
214 if (r < 0)
215 return log_warning_errno(r, "Failed to check validity of domain name '%s', at line %s:%u, ignoring line: %m", domain, path, line);
216 if (r == 0) {
217 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
218 return -EINVAL;
219 }
220
221 r = extract_many_words(&p, NULL, 0, &class, &type, NULL);
222 if (r < 0)
223 return log_warning_errno(r, "Unable to parse class and type in line %s:%u: %m", path, line);
224 if (r != 2) {
225 log_warning("Missing class or type in line %s:%u", path, line);
226 return -EINVAL;
227 }
228
229 if (!strcaseeq(class, "IN")) {
230 log_warning("RR class %s is not supported, ignoring line %s:%u.", class, path, line);
231 return -EINVAL;
232 }
233
234 if (strcaseeq(type, "DS")) {
235 _cleanup_free_ char *key_tag = NULL, *algorithm = NULL, *digest_type = NULL;
236 _cleanup_free_ void *dd = NULL;
237 uint16_t kt;
238 int a, dt;
239 size_t l;
240
241 r = extract_many_words(&p, NULL, 0, &key_tag, &algorithm, &digest_type, NULL);
242 if (r < 0) {
243 log_warning_errno(r, "Failed to parse DS parameters on line %s:%u: %m", path, line);
244 return -EINVAL;
245 }
246 if (r != 3) {
247 log_warning("Missing DS parameters on line %s:%u", path, line);
248 return -EINVAL;
249 }
250
251 r = safe_atou16(key_tag, &kt);
252 if (r < 0)
253 return log_warning_errno(r, "Failed to parse DS key tag %s on line %s:%u: %m", key_tag, path, line);
254
255 a = dnssec_algorithm_from_string(algorithm);
256 if (a < 0) {
257 log_warning("Failed to parse DS algorithm %s on line %s:%u", algorithm, path, line);
258 return -EINVAL;
259 }
260
261 dt = dnssec_digest_from_string(digest_type);
262 if (dt < 0) {
263 log_warning("Failed to parse DS digest type %s on line %s:%u", digest_type, path, line);
264 return -EINVAL;
265 }
266
267 if (isempty(p)) {
268 log_warning("Missing DS digest on line %s:%u", path, line);
269 return -EINVAL;
270 }
271
272 r = unhexmem(p, strlen(p), &dd, &l);
273 if (r < 0) {
274 log_warning("Failed to parse DS digest %s on line %s:%u", p, path, line);
275 return -EINVAL;
276 }
277
278 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, domain);
279 if (!rr)
280 return log_oom();
281
282 rr->ds.key_tag = kt;
283 rr->ds.algorithm = a;
284 rr->ds.digest_type = dt;
285 rr->ds.digest_size = l;
286 rr->ds.digest = TAKE_PTR(dd);
287
288 } else if (strcaseeq(type, "DNSKEY")) {
289 _cleanup_free_ char *flags = NULL, *protocol = NULL, *algorithm = NULL;
290 _cleanup_free_ void *k = NULL;
291 uint16_t f;
292 size_t l;
293 int a;
294
295 r = extract_many_words(&p, NULL, 0, &flags, &protocol, &algorithm, NULL);
296 if (r < 0)
297 return log_warning_errno(r, "Failed to parse DNSKEY parameters on line %s:%u: %m", path, line);
298 if (r != 3) {
299 log_warning("Missing DNSKEY parameters on line %s:%u", path, line);
300 return -EINVAL;
301 }
302
303 if (!streq(protocol, "3")) {
304 log_warning("DNSKEY Protocol is not 3 on line %s:%u", path, line);
305 return -EINVAL;
306 }
307
308 r = safe_atou16(flags, &f);
309 if (r < 0)
310 return log_warning_errno(r, "Failed to parse DNSKEY flags field %s on line %s:%u", flags, path, line);
311 if ((f & DNSKEY_FLAG_ZONE_KEY) == 0) {
312 log_warning("DNSKEY lacks zone key bit set on line %s:%u", path, line);
313 return -EINVAL;
314 }
315 if ((f & DNSKEY_FLAG_REVOKE)) {
316 log_warning("DNSKEY is already revoked on line %s:%u", path, line);
317 return -EINVAL;
318 }
319
320 a = dnssec_algorithm_from_string(algorithm);
321 if (a < 0) {
322 log_warning("Failed to parse DNSKEY algorithm %s on line %s:%u", algorithm, path, line);
323 return -EINVAL;
324 }
325
326 if (isempty(p)) {
327 log_warning("Missing DNSKEY key on line %s:%u", path, line);
328 return -EINVAL;
329 }
330
331 r = unbase64mem(p, strlen(p), &k, &l);
332 if (r < 0)
333 return log_warning_errno(r, "Failed to parse DNSKEY key data %s on line %s:%u", p, path, line);
334
335 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, domain);
336 if (!rr)
337 return log_oom();
338
339 rr->dnskey.flags = f;
340 rr->dnskey.protocol = 3;
341 rr->dnskey.algorithm = a;
342 rr->dnskey.key_size = l;
343 rr->dnskey.key = TAKE_PTR(k);
344
345 } else {
346 log_warning("RR type %s is not supported, ignoring line %s:%u.", type, path, line);
347 return -EINVAL;
348 }
349
350 r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
351 if (r < 0)
352 return log_oom();
353
354 old_answer = hashmap_get(d->positive_by_key, rr->key);
355 answer = dns_answer_ref(old_answer);
356
357 r = dns_answer_add_extend(&answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
358 if (r < 0)
359 return log_error_errno(r, "Failed to add trust anchor RR: %m");
360
361 r = hashmap_replace(d->positive_by_key, rr->key, answer);
362 if (r < 0)
363 return log_error_errno(r, "Failed to add answer to trust anchor: %m");
364
365 old_answer = dns_answer_unref(old_answer);
366 answer = NULL;
367
368 return 0;
369 }
370
371 static int dns_trust_anchor_load_negative(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
372 _cleanup_free_ char *domain = NULL;
373 const char *p = s;
374 int r;
375
376 assert(d);
377 assert(line);
378
379 r = extract_first_word(&p, &domain, NULL, EXTRACT_UNQUOTE);
380 if (r < 0)
381 return log_warning_errno(r, "Unable to parse line %s:%u: %m", path, line);
382
383 r = dns_name_is_valid(domain);
384 if (r < 0)
385 return log_warning_errno(r, "Failed to check validity of domain name '%s', at line %s:%u, ignoring line: %m", domain, path, line);
386 if (r == 0) {
387 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
388 return -EINVAL;
389 }
390
391 if (!isempty(p)) {
392 log_warning("Trailing garbage at line %s:%u, ignoring line.", path, line);
393 return -EINVAL;
394 }
395
396 r = set_ensure_consume(&d->negative_by_name, &dns_name_hash_ops, TAKE_PTR(domain));
397 if (r < 0)
398 return log_oom();
399
400 return 0;
401 }
402
403 static int dns_trust_anchor_load_files(
404 DnsTrustAnchor *d,
405 const char *suffix,
406 int (*loader)(DnsTrustAnchor *d, const char *path, unsigned n, const char *line)) {
407
408 _cleanup_strv_free_ char **files = NULL;
409 char **f;
410 int r;
411
412 assert(d);
413 assert(suffix);
414 assert(loader);
415
416 r = conf_files_list_nulstr(&files, suffix, NULL, 0, trust_anchor_dirs);
417 if (r < 0)
418 return log_error_errno(r, "Failed to enumerate %s trust anchor files: %m", suffix);
419
420 STRV_FOREACH(f, files) {
421 _cleanup_fclose_ FILE *g = NULL;
422 unsigned n = 0;
423
424 g = fopen(*f, "r");
425 if (!g) {
426 if (errno == ENOENT)
427 continue;
428
429 log_warning_errno(errno, "Failed to open '%s', ignoring: %m", *f);
430 continue;
431 }
432
433 for (;;) {
434 _cleanup_free_ char *line = NULL;
435 char *l;
436
437 r = read_line(g, LONG_LINE_MAX, &line);
438 if (r < 0) {
439 log_warning_errno(r, "Failed to read '%s', ignoring: %m", *f);
440 break;
441 }
442 if (r == 0)
443 break;
444
445 n++;
446
447 l = strstrip(line);
448 if (isempty(l))
449 continue;
450
451 if (*l == ';')
452 continue;
453
454 (void) loader(d, *f, n, l);
455 }
456 }
457
458 return 0;
459 }
460
461 static int domain_name_cmp(char * const *a, char * const *b) {
462 return dns_name_compare_func(*a, *b);
463 }
464
465 static int dns_trust_anchor_dump(DnsTrustAnchor *d) {
466 DnsAnswer *a;
467
468 assert(d);
469
470 if (hashmap_isempty(d->positive_by_key))
471 log_info("No positive trust anchors defined.");
472 else {
473 log_info("Positive Trust Anchors:");
474 HASHMAP_FOREACH(a, d->positive_by_key) {
475 DnsResourceRecord *rr;
476
477 DNS_ANSWER_FOREACH(rr, a)
478 log_info("%s", dns_resource_record_to_string(rr));
479 }
480 }
481
482 if (set_isempty(d->negative_by_name))
483 log_info("No negative trust anchors defined.");
484 else {
485 _cleanup_free_ char **l = NULL, *j = NULL;
486
487 l = set_get_strv(d->negative_by_name);
488 if (!l)
489 return log_oom();
490
491 typesafe_qsort(l, set_size(d->negative_by_name), domain_name_cmp);
492
493 j = strv_join(l, " ");
494 if (!j)
495 return log_oom();
496
497 log_info("Negative trust anchors: %s", j);
498 }
499
500 return 0;
501 }
502
503 int dns_trust_anchor_load(DnsTrustAnchor *d) {
504 int r;
505
506 assert(d);
507
508 /* If loading things from disk fails, we don't consider this fatal */
509 (void) dns_trust_anchor_load_files(d, ".positive", dns_trust_anchor_load_positive);
510 (void) dns_trust_anchor_load_files(d, ".negative", dns_trust_anchor_load_negative);
511
512 /* However, if the built-in DS fails, then we have a problem. */
513 r = dns_trust_anchor_add_builtin_positive(d);
514 if (r < 0)
515 return log_error_errno(r, "Failed to add built-in positive trust anchor: %m");
516
517 r = dns_trust_anchor_add_builtin_negative(d);
518 if (r < 0)
519 return log_error_errno(r, "Failed to add built-in negative trust anchor: %m");
520
521 dns_trust_anchor_dump(d);
522
523 return 0;
524 }
525
526 void dns_trust_anchor_flush(DnsTrustAnchor *d) {
527 assert(d);
528
529 d->positive_by_key = hashmap_free_with_destructor(d->positive_by_key, dns_answer_unref);
530 d->revoked_by_rr = set_free_with_destructor(d->revoked_by_rr, dns_resource_record_unref);
531 d->negative_by_name = set_free_free(d->negative_by_name);
532 }
533
534 int dns_trust_anchor_lookup_positive(DnsTrustAnchor *d, const DnsResourceKey *key, DnsAnswer **ret) {
535 DnsAnswer *a;
536
537 assert(d);
538 assert(key);
539 assert(ret);
540
541 /* We only serve DS and DNSKEY RRs. */
542 if (!IN_SET(key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
543 return 0;
544
545 a = hashmap_get(d->positive_by_key, key);
546 if (!a)
547 return 0;
548
549 *ret = dns_answer_ref(a);
550 return 1;
551 }
552
553 int dns_trust_anchor_lookup_negative(DnsTrustAnchor *d, const char *name) {
554 int r;
555
556 assert(d);
557 assert(name);
558
559 for (;;) {
560 /* If the domain is listed as-is in the NTA database, then that counts */
561 if (set_contains(d->negative_by_name, name))
562 return true;
563
564 /* If the domain isn't listed as NTA, but is listed as positive trust anchor, then that counts. See RFC
565 * 7646, section 1.1 */
566 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DS, name)))
567 return false;
568
569 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_KEY, name)))
570 return false;
571
572 /* And now, let's look at the parent, and check that too */
573 r = dns_name_parent(&name);
574 if (r < 0)
575 return r;
576 if (r == 0)
577 break;
578 }
579
580 return false;
581 }
582
583 static int dns_trust_anchor_revoked_put(DnsTrustAnchor *d, DnsResourceRecord *rr) {
584 int r;
585
586 assert(d);
587
588 r = set_ensure_put(&d->revoked_by_rr, &dns_resource_record_hash_ops, rr);
589 if (r < 0)
590 return r;
591 if (r > 0)
592 dns_resource_record_ref(rr);
593
594 return r;
595 }
596
597 static int dns_trust_anchor_remove_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) {
598 _cleanup_(dns_answer_unrefp) DnsAnswer *new_answer = NULL;
599 DnsAnswer *old_answer;
600 int r;
601
602 /* Remember that this is a revoked trust anchor RR */
603 r = dns_trust_anchor_revoked_put(d, rr);
604 if (r < 0)
605 return r;
606
607 /* Remove this from the positive trust anchor */
608 old_answer = hashmap_get(d->positive_by_key, rr->key);
609 if (!old_answer)
610 return 0;
611
612 new_answer = dns_answer_ref(old_answer);
613
614 r = dns_answer_remove_by_rr(&new_answer, rr);
615 if (r <= 0)
616 return r;
617
618 /* We found the key! Warn the user */
619 log_struct(LOG_WARNING,
620 "MESSAGE_ID=" SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED_STR,
621 LOG_MESSAGE("DNSSEC trust anchor %s has been revoked.\n"
622 "Please update the trust anchor, or upgrade your operating system.",
623 strna(dns_resource_record_to_string(rr))),
624 "TRUST_ANCHOR=%s", dns_resource_record_to_string(rr));
625
626 if (dns_answer_size(new_answer) <= 0) {
627 assert_se(hashmap_remove(d->positive_by_key, rr->key) == old_answer);
628 dns_answer_unref(old_answer);
629 return 1;
630 }
631
632 r = hashmap_replace(d->positive_by_key, new_answer->items[0].rr->key, new_answer);
633 if (r < 0)
634 return r;
635
636 new_answer = NULL;
637 dns_answer_unref(old_answer);
638 return 1;
639 }
640
641 static int dns_trust_anchor_check_revoked_one(DnsTrustAnchor *d, DnsResourceRecord *revoked_dnskey) {
642 DnsAnswer *a;
643 int r;
644
645 assert(d);
646 assert(revoked_dnskey);
647 assert(revoked_dnskey->key->type == DNS_TYPE_DNSKEY);
648 assert(revoked_dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE);
649
650 a = hashmap_get(d->positive_by_key, revoked_dnskey->key);
651 if (a) {
652 DnsResourceRecord *anchor;
653
654 /* First, look for the precise DNSKEY in our trust anchor database */
655
656 DNS_ANSWER_FOREACH(anchor, a) {
657
658 if (anchor->dnskey.protocol != revoked_dnskey->dnskey.protocol)
659 continue;
660
661 if (anchor->dnskey.algorithm != revoked_dnskey->dnskey.algorithm)
662 continue;
663
664 if (anchor->dnskey.key_size != revoked_dnskey->dnskey.key_size)
665 continue;
666
667 /* Note that we allow the REVOKE bit to be
668 * different! It will be set in the revoked
669 * key, but unset in our version of it */
670 if (((anchor->dnskey.flags ^ revoked_dnskey->dnskey.flags) | DNSKEY_FLAG_REVOKE) != DNSKEY_FLAG_REVOKE)
671 continue;
672
673 if (memcmp(anchor->dnskey.key, revoked_dnskey->dnskey.key, anchor->dnskey.key_size) != 0)
674 continue;
675
676 dns_trust_anchor_remove_revoked(d, anchor);
677 break;
678 }
679 }
680
681 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)));
682 if (a) {
683 DnsResourceRecord *anchor;
684
685 /* Second, look for DS RRs matching this DNSKEY in our trust anchor database */
686
687 DNS_ANSWER_FOREACH(anchor, a) {
688
689 /* We set mask_revoke to true here, since our
690 * DS fingerprint will be the one of the
691 * unrevoked DNSKEY, but the one we got passed
692 * here has the bit set. */
693 r = dnssec_verify_dnskey_by_ds(revoked_dnskey, anchor, true);
694 if (r < 0)
695 return r;
696 if (r == 0)
697 continue;
698
699 dns_trust_anchor_remove_revoked(d, anchor);
700 break;
701 }
702 }
703
704 return 0;
705 }
706
707 int dns_trust_anchor_check_revoked(DnsTrustAnchor *d, DnsResourceRecord *dnskey, DnsAnswer *rrs) {
708 DnsResourceRecord *rrsig;
709 int r;
710
711 assert(d);
712 assert(dnskey);
713
714 /* Looks if "dnskey" is a self-signed RR that has been revoked
715 * and matches one of our trust anchor entries. If so, removes
716 * it from the trust anchor and returns > 0. */
717
718 if (dnskey->key->type != DNS_TYPE_DNSKEY)
719 return 0;
720
721 /* Is this DNSKEY revoked? */
722 if ((dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE) == 0)
723 return 0;
724
725 /* Could this be interesting to us at all? If not,
726 * there's no point in looking for and verifying a
727 * self-signed RRSIG. */
728 if (!dns_trust_anchor_knows_domain_positive(d, dns_resource_key_name(dnskey->key)))
729 return 0;
730
731 /* Look for a self-signed RRSIG in the other rrs belonging to this DNSKEY */
732 DNS_ANSWER_FOREACH(rrsig, rrs) {
733 DnssecResult result;
734
735 if (rrsig->key->type != DNS_TYPE_RRSIG)
736 continue;
737
738 r = dnssec_rrsig_match_dnskey(rrsig, dnskey, true);
739 if (r < 0)
740 return r;
741 if (r == 0)
742 continue;
743
744 r = dnssec_verify_rrset(rrs, dnskey->key, rrsig, dnskey, USEC_INFINITY, &result);
745 if (r < 0)
746 return r;
747 if (result != DNSSEC_VALIDATED)
748 continue;
749
750 /* Bingo! This is a revoked self-signed DNSKEY. Let's
751 * see if this precise one exists in our trust anchor
752 * database, too. */
753 r = dns_trust_anchor_check_revoked_one(d, dnskey);
754 if (r < 0)
755 return r;
756
757 return 1;
758 }
759
760 return 0;
761 }
762
763 int dns_trust_anchor_is_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) {
764 assert(d);
765
766 if (!IN_SET(rr->key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
767 return 0;
768
769 return set_contains(d->revoked_by_rr, rr);
770 }