]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-trust-anchor.c
Merge pull request #5373 from poettering/coredump-timestamp-fixes
[thirdparty/systemd.git] / src / resolve / resolved-dns-trust-anchor.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2015 Lennart Poettering
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include <sd-messages.h>
21
22 #include "alloc-util.h"
23 #include "conf-files.h"
24 #include "def.h"
25 #include "dns-domain.h"
26 #include "fd-util.h"
27 #include "fileio.h"
28 #include "hexdecoct.h"
29 #include "parse-util.h"
30 #include "resolved-dns-trust-anchor.h"
31 #include "resolved-dns-dnssec.h"
32 #include "set.h"
33 #include "string-util.h"
34 #include "strv.h"
35
36 static const char trust_anchor_dirs[] = CONF_PATHS_NULSTR("dnssec-trust-anchors.d");
37
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 };
42
43 static bool dns_trust_anchor_knows_domain_positive(DnsTrustAnchor *d, const char *name) {
44 assert(d);
45
46 /* Returns true if there's an entry for the specified domain
47 * name in our trust anchor */
48
49 return
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));
52 }
53
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;
57 int r;
58
59 assert(d);
60
61 r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
62 if (r < 0)
63 return r;
64
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
68 * data. */
69 if (dns_trust_anchor_knows_domain_positive(d, "."))
70 return 0;
71
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, "");
74 if (!rr)
75 return -ENOMEM;
76
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);
82 if (!rr->ds.digest)
83 return -ENOMEM;
84
85 answer = dns_answer_new(1);
86 if (!answer)
87 return -ENOMEM;
88
89 r = dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
90 if (r < 0)
91 return r;
92
93 r = hashmap_put(d->positive_by_key, rr->key, answer);
94 if (r < 0)
95 return r;
96
97 answer = NULL;
98 return 0;
99 }
100
101 static int dns_trust_anchor_add_builtin_negative(DnsTrustAnchor *d) {
102
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 */
106 "test\0"
107
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 */
111 "10.in-addr.arpa\0"
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"
129
130 /* The same, but for IPv6. */
131 "d.f.ip6.arpa\0"
132
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
137 * suggests so.) */
138 "local\0"
139
140 /* These two are well known, popular private zone
141 * TLDs, that are blocked from delegation, according
142 * to:
143 * http://icannwiki.com/Name_Collision#NGPC_Resolution
144 *
145 * There's also ongoing work on making this official
146 * in an RRC:
147 * https://www.ietf.org/archive/id/draft-chapin-additional-reserved-tlds-02.txt */
148 "home\0"
149 "corp\0"
150
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 */
154 "lan\0"
155 "intranet\0"
156 "internal\0"
157 "private\0";
158
159 const char *name;
160 int r;
161
162 assert(d);
163
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. */
167
168 if (set_size(d->negative_by_name) > 0)
169 return 0;
170
171 r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops);
172 if (r < 0)
173 return r;
174
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
178 * unsigned. */
179
180 NULSTR_FOREACH(name, private_domains) {
181
182 if (dns_trust_anchor_knows_domain_positive(d, name))
183 continue;
184
185 r = set_put_strdup(d->negative_by_name, name);
186 if (r < 0)
187 return r;
188 }
189
190 return 0;
191 }
192
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;
198 const char *p = s;
199 int r;
200
201 assert(d);
202 assert(line);
203
204 r = extract_first_word(&p, &domain, NULL, EXTRACT_QUOTES);
205 if (r < 0)
206 return log_warning_errno(r, "Unable to parse domain in line %s:%u: %m", path, line);
207
208 if (!dns_name_is_valid(domain)) {
209 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
210 return -EINVAL;
211 }
212
213 r = extract_many_words(&p, NULL, 0, &class, &type, NULL);
214 if (r < 0)
215 return log_warning_errno(r, "Unable to parse class and type in line %s:%u: %m", path, line);
216 if (r != 2) {
217 log_warning("Missing class or type in line %s:%u", path, line);
218 return -EINVAL;
219 }
220
221 if (!strcaseeq(class, "IN")) {
222 log_warning("RR class %s is not supported, ignoring line %s:%u.", class, path, line);
223 return -EINVAL;
224 }
225
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;
229 uint16_t kt;
230 int a, dt;
231 size_t l;
232
233 r = extract_many_words(&p, NULL, 0, &key_tag, &algorithm, &digest_type, &digest, NULL);
234 if (r < 0) {
235 log_warning_errno(r, "Failed to parse DS parameters on line %s:%u: %m", path, line);
236 return -EINVAL;
237 }
238 if (r != 4) {
239 log_warning("Missing DS parameters on line %s:%u", path, line);
240 return -EINVAL;
241 }
242
243 r = safe_atou16(key_tag, &kt);
244 if (r < 0)
245 return log_warning_errno(r, "Failed to parse DS key tag %s on line %s:%u: %m", key_tag, path, line);
246
247 a = dnssec_algorithm_from_string(algorithm);
248 if (a < 0) {
249 log_warning("Failed to parse DS algorithm %s on line %s:%u", algorithm, path, line);
250 return -EINVAL;
251 }
252
253 dt = dnssec_digest_from_string(digest_type);
254 if (dt < 0) {
255 log_warning("Failed to parse DS digest type %s on line %s:%u", digest_type, path, line);
256 return -EINVAL;
257 }
258
259 r = unhexmem(digest, strlen(digest), &dd, &l);
260 if (r < 0) {
261 log_warning("Failed to parse DS digest %s on line %s:%u", digest, path, line);
262 return -EINVAL;
263 }
264
265 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, domain);
266 if (!rr)
267 return log_oom();
268
269 rr->ds.key_tag = kt;
270 rr->ds.algorithm = a;
271 rr->ds.digest_type = dt;
272 rr->ds.digest_size = l;
273 rr->ds.digest = dd;
274 dd = NULL;
275
276 } else if (strcaseeq(type, "DNSKEY")) {
277 _cleanup_free_ char *flags = NULL, *protocol = NULL, *algorithm = NULL, *key = NULL;
278 _cleanup_free_ void *k = NULL;
279 uint16_t f;
280 size_t l;
281 int a;
282
283 r = extract_many_words(&p, NULL, 0, &flags, &protocol, &algorithm, &key, NULL);
284 if (r < 0)
285 return log_warning_errno(r, "Failed to parse DNSKEY parameters on line %s:%u: %m", path, line);
286 if (r != 4) {
287 log_warning("Missing DNSKEY parameters on line %s:%u", path, line);
288 return -EINVAL;
289 }
290
291 if (!streq(protocol, "3")) {
292 log_warning("DNSKEY Protocol is not 3 on line %s:%u", path, line);
293 return -EINVAL;
294 }
295
296 r = safe_atou16(flags, &f);
297 if (r < 0)
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);
301 return -EINVAL;
302 }
303 if ((f & DNSKEY_FLAG_REVOKE)) {
304 log_warning("DNSKEY is already revoked on line %s:%u", path, line);
305 return -EINVAL;
306 }
307
308 a = dnssec_algorithm_from_string(algorithm);
309 if (a < 0) {
310 log_warning("Failed to parse DNSKEY algorithm %s on line %s:%u", algorithm, path, line);
311 return -EINVAL;
312 }
313
314 r = unbase64mem(key, strlen(key), &k, &l);
315 if (r < 0)
316 return log_warning_errno(r, "Failed to parse DNSKEY key data %s on line %s:%u", key, path, line);
317
318 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, domain);
319 if (!rr)
320 return log_oom();
321
322 rr->dnskey.flags = f;
323 rr->dnskey.protocol = 3;
324 rr->dnskey.algorithm = a;
325 rr->dnskey.key_size = l;
326 rr->dnskey.key = k;
327 k = NULL;
328
329 } else {
330 log_warning("RR type %s is not supported, ignoring line %s:%u.", type, path, line);
331 return -EINVAL;
332 }
333
334 if (!isempty(p)) {
335 log_warning("Trailing garbage on line %s:%u, ignoring line.", path, line);
336 return -EINVAL;
337 }
338
339 r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
340 if (r < 0)
341 return log_oom();
342
343 old_answer = hashmap_get(d->positive_by_key, rr->key);
344 answer = dns_answer_ref(old_answer);
345
346 r = dns_answer_add_extend(&answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
347 if (r < 0)
348 return log_error_errno(r, "Failed to add trust anchor RR: %m");
349
350 r = hashmap_replace(d->positive_by_key, rr->key, answer);
351 if (r < 0)
352 return log_error_errno(r, "Failed to add answer to trust anchor: %m");
353
354 old_answer = dns_answer_unref(old_answer);
355 answer = NULL;
356
357 return 0;
358 }
359
360 static int dns_trust_anchor_load_negative(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
361 _cleanup_free_ char *domain = NULL;
362 const char *p = s;
363 int r;
364
365 assert(d);
366 assert(line);
367
368 r = extract_first_word(&p, &domain, NULL, EXTRACT_QUOTES);
369 if (r < 0)
370 return log_warning_errno(r, "Unable to parse line %s:%u: %m", path, line);
371
372 if (!dns_name_is_valid(domain)) {
373 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
374 return -EINVAL;
375 }
376
377 if (!isempty(p)) {
378 log_warning("Trailing garbage at line %s:%u, ignoring line.", path, line);
379 return -EINVAL;
380 }
381
382 r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops);
383 if (r < 0)
384 return log_oom();
385
386 r = set_put(d->negative_by_name, domain);
387 if (r < 0)
388 return log_oom();
389 if (r > 0)
390 domain = NULL;
391
392 return 0;
393 }
394
395 static int dns_trust_anchor_load_files(
396 DnsTrustAnchor *d,
397 const char *suffix,
398 int (*loader)(DnsTrustAnchor *d, const char *path, unsigned n, const char *line)) {
399
400 _cleanup_strv_free_ char **files = NULL;
401 char **f;
402 int r;
403
404 assert(d);
405 assert(suffix);
406 assert(loader);
407
408 r = conf_files_list_nulstr(&files, suffix, NULL, trust_anchor_dirs);
409 if (r < 0)
410 return log_error_errno(r, "Failed to enumerate %s trust anchor files: %m", suffix);
411
412 STRV_FOREACH(f, files) {
413 _cleanup_fclose_ FILE *g = NULL;
414 char line[LINE_MAX];
415 unsigned n = 0;
416
417 g = fopen(*f, "r");
418 if (!g) {
419 if (errno == ENOENT)
420 continue;
421
422 log_warning_errno(errno, "Failed to open %s: %m", *f);
423 continue;
424 }
425
426 FOREACH_LINE(line, g, log_warning_errno(errno, "Failed to read %s, ignoring: %m", *f)) {
427 char *l;
428
429 n++;
430
431 l = strstrip(line);
432 if (isempty(l))
433 continue;
434
435 if (*l == ';')
436 continue;
437
438 (void) loader(d, *f, n, l);
439 }
440 }
441
442 return 0;
443 }
444
445 static int domain_name_cmp(const void *a, const void *b) {
446 char **x = (char**) a, **y = (char**) b;
447
448 return dns_name_compare_func(*x, *y);
449 }
450
451 static int dns_trust_anchor_dump(DnsTrustAnchor *d) {
452 DnsAnswer *a;
453 Iterator i;
454
455 assert(d);
456
457 if (hashmap_isempty(d->positive_by_key))
458 log_info("No positive trust anchors defined.");
459 else {
460 log_info("Positive Trust Anchors:");
461 HASHMAP_FOREACH(a, d->positive_by_key, i) {
462 DnsResourceRecord *rr;
463
464 DNS_ANSWER_FOREACH(rr, a)
465 log_info("%s", dns_resource_record_to_string(rr));
466 }
467 }
468
469 if (set_isempty(d->negative_by_name))
470 log_info("No negative trust anchors defined.");
471 else {
472 _cleanup_free_ char **l = NULL, *j = NULL;
473
474 l = set_get_strv(d->negative_by_name);
475 if (!l)
476 return log_oom();
477
478 qsort_safe(l, set_size(d->negative_by_name), sizeof(char*), domain_name_cmp);
479
480 j = strv_join(l, " ");
481 if (!j)
482 return log_oom();
483
484 log_info("Negative trust anchors: %s", j);
485 }
486
487 return 0;
488 }
489
490 int dns_trust_anchor_load(DnsTrustAnchor *d) {
491 int r;
492
493 assert(d);
494
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);
498
499 /* However, if the built-in DS fails, then we have a problem. */
500 r = dns_trust_anchor_add_builtin_positive(d);
501 if (r < 0)
502 return log_error_errno(r, "Failed to add built-in positive trust anchor: %m");
503
504 r = dns_trust_anchor_add_builtin_negative(d);
505 if (r < 0)
506 return log_error_errno(r, "Failed to add built-in negative trust anchor: %m");
507
508 dns_trust_anchor_dump(d);
509
510 return 0;
511 }
512
513 void dns_trust_anchor_flush(DnsTrustAnchor *d) {
514 DnsAnswer *a;
515 DnsResourceRecord *rr;
516
517 assert(d);
518
519 while ((a = hashmap_steal_first(d->positive_by_key)))
520 dns_answer_unref(a);
521 d->positive_by_key = hashmap_free(d->positive_by_key);
522
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);
526
527 d->negative_by_name = set_free_free(d->negative_by_name);
528 }
529
530 int dns_trust_anchor_lookup_positive(DnsTrustAnchor *d, const DnsResourceKey *key, DnsAnswer **ret) {
531 DnsAnswer *a;
532
533 assert(d);
534 assert(key);
535 assert(ret);
536
537 /* We only serve DS and DNSKEY RRs. */
538 if (!IN_SET(key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
539 return 0;
540
541 a = hashmap_get(d->positive_by_key, key);
542 if (!a)
543 return 0;
544
545 *ret = dns_answer_ref(a);
546 return 1;
547 }
548
549 int dns_trust_anchor_lookup_negative(DnsTrustAnchor *d, const char *name) {
550 int r;
551
552 assert(d);
553 assert(name);
554
555 for (;;) {
556 /* If the domain is listed as-is in the NTA database, then that counts */
557 if (set_contains(d->negative_by_name, name))
558 return true;
559
560 /* If the domain isn't listed as NTA, but is listed as positive trust anchor, then that counts. See RFC
561 * 7646, section 1.1 */
562 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DS, name)))
563 return false;
564
565 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_KEY, name)))
566 return false;
567
568 /* And now, let's look at the parent, and check that too */
569 r = dns_name_parent(&name);
570 if (r < 0)
571 return r;
572 if (r == 0)
573 break;
574 }
575
576 return false;
577 }
578
579 static int dns_trust_anchor_revoked_put(DnsTrustAnchor *d, DnsResourceRecord *rr) {
580 int r;
581
582 assert(d);
583
584 r = set_ensure_allocated(&d->revoked_by_rr, &dns_resource_record_hash_ops);
585 if (r < 0)
586 return r;
587
588 r = set_put(d->revoked_by_rr, 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. Please update the trust anchor, or upgrade your operating system."), strna(dns_resource_record_to_string(rr)),
622 "TRUST_ANCHOR=%s", dns_resource_record_to_string(rr),
623 NULL);
624
625 if (dns_answer_size(new_answer) <= 0) {
626 assert_se(hashmap_remove(d->positive_by_key, rr->key) == old_answer);
627 dns_answer_unref(old_answer);
628 return 1;
629 }
630
631 r = hashmap_replace(d->positive_by_key, new_answer->items[0].rr->key, new_answer);
632 if (r < 0)
633 return r;
634
635 new_answer = NULL;
636 dns_answer_unref(old_answer);
637 return 1;
638 }
639
640 static int dns_trust_anchor_check_revoked_one(DnsTrustAnchor *d, DnsResourceRecord *revoked_dnskey) {
641 DnsAnswer *a;
642 int r;
643
644 assert(d);
645 assert(revoked_dnskey);
646 assert(revoked_dnskey->key->type == DNS_TYPE_DNSKEY);
647 assert(revoked_dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE);
648
649 a = hashmap_get(d->positive_by_key, revoked_dnskey->key);
650 if (a) {
651 DnsResourceRecord *anchor;
652
653 /* First, look for the precise DNSKEY in our trust anchor database */
654
655 DNS_ANSWER_FOREACH(anchor, a) {
656
657 if (anchor->dnskey.protocol != revoked_dnskey->dnskey.protocol)
658 continue;
659
660 if (anchor->dnskey.algorithm != revoked_dnskey->dnskey.algorithm)
661 continue;
662
663 if (anchor->dnskey.key_size != revoked_dnskey->dnskey.key_size)
664 continue;
665
666 /* Note that we allow the REVOKE bit to be
667 * different! It will be set in the revoked
668 * key, but unset in our version of it */
669 if (((anchor->dnskey.flags ^ revoked_dnskey->dnskey.flags) | DNSKEY_FLAG_REVOKE) != DNSKEY_FLAG_REVOKE)
670 continue;
671
672 if (memcmp(anchor->dnskey.key, revoked_dnskey->dnskey.key, anchor->dnskey.key_size) != 0)
673 continue;
674
675 dns_trust_anchor_remove_revoked(d, anchor);
676 break;
677 }
678 }
679
680 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)));
681 if (a) {
682 DnsResourceRecord *anchor;
683
684 /* Second, look for DS RRs matching this DNSKEY in our trust anchor database */
685
686 DNS_ANSWER_FOREACH(anchor, a) {
687
688 /* We set mask_revoke to true here, since our
689 * DS fingerprint will be the one of the
690 * unrevoked DNSKEY, but the one we got passed
691 * here has the bit set. */
692 r = dnssec_verify_dnskey_by_ds(revoked_dnskey, anchor, true);
693 if (r < 0)
694 return r;
695 if (r == 0)
696 continue;
697
698 dns_trust_anchor_remove_revoked(d, anchor);
699 break;
700 }
701 }
702
703 return 0;
704 }
705
706 int dns_trust_anchor_check_revoked(DnsTrustAnchor *d, DnsResourceRecord *dnskey, DnsAnswer *rrs) {
707 DnsResourceRecord *rrsig;
708 int r;
709
710 assert(d);
711 assert(dnskey);
712
713 /* Looks if "dnskey" is a self-signed RR that has been revoked
714 * and matches one of our trust anchor entries. If so, removes
715 * it from the trust anchor and returns > 0. */
716
717 if (dnskey->key->type != DNS_TYPE_DNSKEY)
718 return 0;
719
720 /* Is this DNSKEY revoked? */
721 if ((dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE) == 0)
722 return 0;
723
724 /* Could this be interesting to us at all? If not,
725 * there's no point in looking for and verifying a
726 * self-signed RRSIG. */
727 if (!dns_trust_anchor_knows_domain_positive(d, dns_resource_key_name(dnskey->key)))
728 return 0;
729
730 /* Look for a self-signed RRSIG in the other rrs belonging to this DNSKEY */
731 DNS_ANSWER_FOREACH(rrsig, rrs) {
732 DnssecResult result;
733
734 if (rrsig->key->type != DNS_TYPE_RRSIG)
735 continue;
736
737 r = dnssec_rrsig_match_dnskey(rrsig, dnskey, true);
738 if (r < 0)
739 return r;
740 if (r == 0)
741 continue;
742
743 r = dnssec_verify_rrset(rrs, dnskey->key, rrsig, dnskey, USEC_INFINITY, &result);
744 if (r < 0)
745 return r;
746 if (result != DNSSEC_VALIDATED)
747 continue;
748
749 /* Bingo! This is a revoked self-signed DNSKEY. Let's
750 * see if this precise one exists in our trust anchor
751 * database, too. */
752 r = dns_trust_anchor_check_revoked_one(d, dnskey);
753 if (r < 0)
754 return r;
755
756 return 1;
757 }
758
759 return 0;
760 }
761
762 int dns_trust_anchor_is_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) {
763 assert(d);
764
765 if (!IN_SET(rr->key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
766 return 0;
767
768 return set_contains(d->revoked_by_rr, rr);
769 }