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