]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-trust-anchor.c
69e363733025ecfe4423908dfe787a98985d11c9
[thirdparty/systemd.git] / src / resolve / resolved-dns-trust-anchor.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2015 Lennart Poettering
6 ***/
7
8 #include "sd-messages.h"
9
10 #include "alloc-util.h"
11 #include "conf-files.h"
12 #include "def.h"
13 #include "dns-domain.h"
14 #include "fd-util.h"
15 #include "fileio.h"
16 #include "hexdecoct.h"
17 #include "parse-util.h"
18 #include "resolved-dns-trust-anchor.h"
19 #include "resolved-dns-dnssec.h"
20 #include "set.h"
21 #include "string-util.h"
22 #include "strv.h"
23
24 static const char trust_anchor_dirs[] = CONF_PATHS_NULSTR("dnssec-trust-anchors.d");
25
26 /* The first DS RR from https://data.iana.org/root-anchors/root-anchors.xml, retrieved December 2015 */
27 static const uint8_t root_digest1[] =
28 { 0x49, 0xAA, 0xC1, 0x1D, 0x7B, 0x6F, 0x64, 0x46, 0x70, 0x2E, 0x54, 0xA1, 0x60, 0x73, 0x71, 0x60,
29 0x7A, 0x1A, 0x41, 0x85, 0x52, 0x00, 0xFD, 0x2C, 0xE1, 0xCD, 0xDE, 0x32, 0xF2, 0x4E, 0x8F, 0xB5 };
30
31 /* The second DS RR from https://data.iana.org/root-anchors/root-anchors.xml, retrieved February 2017 */
32 static const uint8_t root_digest2[] =
33 { 0xE0, 0x6D, 0x44, 0xB8, 0x0B, 0x8F, 0x1D, 0x39, 0xA9, 0x5C, 0x0B, 0x0D, 0x7C, 0x65, 0xD0, 0x84,
34 0x58, 0xE8, 0x80, 0x40, 0x9B, 0xBC, 0x68, 0x34, 0x57, 0x10, 0x42, 0x37, 0xC7, 0xF8, 0xEC, 0x8D };
35
36 static bool dns_trust_anchor_knows_domain_positive(DnsTrustAnchor *d, const char *name) {
37 assert(d);
38
39 /* Returns true if there's an entry for the specified domain
40 * name in our trust anchor */
41
42 return
43 hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DNSKEY, name)) ||
44 hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DS, name));
45 }
46
47 static int add_root_ksk(
48 DnsAnswer *answer,
49 DnsResourceKey *key,
50 uint16_t key_tag,
51 uint8_t algorithm,
52 uint8_t digest_type,
53 const void *digest,
54 size_t digest_size) {
55
56 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
57 int r;
58
59 rr = dns_resource_record_new(key);
60 if (!rr)
61 return -ENOMEM;
62
63 rr->ds.key_tag = key_tag;
64 rr->ds.algorithm = algorithm;
65 rr->ds.digest_type = digest_type;
66 rr->ds.digest_size = digest_size;
67 rr->ds.digest = memdup(digest, rr->ds.digest_size);
68 if (!rr->ds.digest)
69 return -ENOMEM;
70
71 r = dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
72 if (r < 0)
73 return r;
74
75 return 0;
76 }
77
78 static int dns_trust_anchor_add_builtin_positive(DnsTrustAnchor *d) {
79 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
80 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
81 int r;
82
83 assert(d);
84
85 r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
86 if (r < 0)
87 return r;
88
89 /* Only add the built-in trust anchor if there's neither a DS nor a DNSKEY defined for the root domain. That
90 * way users have an easy way to override the root domain DS/DNSKEY data. */
91 if (dns_trust_anchor_knows_domain_positive(d, "."))
92 return 0;
93
94 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_DS, "");
95 if (!key)
96 return -ENOMEM;
97
98 answer = dns_answer_new(2);
99 if (!answer)
100 return -ENOMEM;
101
102 /* Add the two RRs from https://data.iana.org/root-anchors/root-anchors.xml */
103 r = add_root_ksk(answer, key, 19036, DNSSEC_ALGORITHM_RSASHA256, DNSSEC_DIGEST_SHA256, root_digest1, sizeof(root_digest1));
104 if (r < 0)
105 return r;
106
107 r = add_root_ksk(answer, key, 20326, DNSSEC_ALGORITHM_RSASHA256, DNSSEC_DIGEST_SHA256, root_digest2, sizeof(root_digest2));
108 if (r < 0)
109 return r;
110
111 r = hashmap_put(d->positive_by_key, key, answer);
112 if (r < 0)
113 return r;
114
115 answer = NULL;
116 return 0;
117 }
118
119 static int dns_trust_anchor_add_builtin_negative(DnsTrustAnchor *d) {
120
121 static const char private_domains[] =
122 /* RFC 6761 says that .test is a special domain for
123 * testing and not to be installed in the root zone */
124 "test\0"
125
126 /* RFC 6761 says that these reverse IP lookup ranges
127 * are for private addresses, and hence should not
128 * show up in the root zone */
129 "10.in-addr.arpa\0"
130 "16.172.in-addr.arpa\0"
131 "17.172.in-addr.arpa\0"
132 "18.172.in-addr.arpa\0"
133 "19.172.in-addr.arpa\0"
134 "20.172.in-addr.arpa\0"
135 "21.172.in-addr.arpa\0"
136 "22.172.in-addr.arpa\0"
137 "23.172.in-addr.arpa\0"
138 "24.172.in-addr.arpa\0"
139 "25.172.in-addr.arpa\0"
140 "26.172.in-addr.arpa\0"
141 "27.172.in-addr.arpa\0"
142 "28.172.in-addr.arpa\0"
143 "29.172.in-addr.arpa\0"
144 "30.172.in-addr.arpa\0"
145 "31.172.in-addr.arpa\0"
146 "168.192.in-addr.arpa\0"
147
148 /* The same, but for IPv6. */
149 "d.f.ip6.arpa\0"
150
151 /* RFC 6762 reserves the .local domain for Multicast
152 * DNS, it hence cannot appear in the root zone. (Note
153 * that we by default do not route .local traffic to
154 * DNS anyway, except when a configured search domain
155 * suggests so.) */
156 "local\0"
157
158 /* These two are well known, popular private zone
159 * TLDs, that are blocked from delegation, according
160 * to:
161 * http://icannwiki.com/Name_Collision#NGPC_Resolution
162 *
163 * There's also ongoing work on making this official
164 * in an RRC:
165 * https://www.ietf.org/archive/id/draft-chapin-additional-reserved-tlds-02.txt */
166 "home\0"
167 "corp\0"
168
169 /* The following four TLDs are suggested for private
170 * zones in RFC 6762, Appendix G, and are hence very
171 * unlikely to be made official TLDs any day soon */
172 "lan\0"
173 "intranet\0"
174 "internal\0"
175 "private\0";
176
177 const char *name;
178 int r;
179
180 assert(d);
181
182 /* Only add the built-in trust anchor if there's no negative
183 * trust anchor defined at all. This enables easy overriding
184 * of negative trust anchors. */
185
186 if (set_size(d->negative_by_name) > 0)
187 return 0;
188
189 r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops);
190 if (r < 0)
191 return r;
192
193 /* We add a couple of domains as default negative trust
194 * anchors, where it's very unlikely they will be installed in
195 * the root zone. If they exist they must be private, and thus
196 * unsigned. */
197
198 NULSTR_FOREACH(name, private_domains) {
199
200 if (dns_trust_anchor_knows_domain_positive(d, name))
201 continue;
202
203 r = set_put_strdup(d->negative_by_name, name);
204 if (r < 0)
205 return r;
206 }
207
208 return 0;
209 }
210
211 static int dns_trust_anchor_load_positive(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
212 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
213 _cleanup_free_ char *domain = NULL, *class = NULL, *type = NULL;
214 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
215 DnsAnswer *old_answer = NULL;
216 const char *p = s;
217 int r;
218
219 assert(d);
220 assert(line);
221
222 r = extract_first_word(&p, &domain, NULL, EXTRACT_QUOTES);
223 if (r < 0)
224 return log_warning_errno(r, "Unable to parse domain in line %s:%u: %m", path, line);
225
226 if (!dns_name_is_valid(domain)) {
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);
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_QUOTES);
390 if (r < 0)
391 return log_warning_errno(r, "Unable to parse line %s:%u: %m", path, line);
392
393 if (!dns_name_is_valid(domain)) {
394 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
395 return -EINVAL;
396 }
397
398 if (!isempty(p)) {
399 log_warning("Trailing garbage at line %s:%u, ignoring line.", path, line);
400 return -EINVAL;
401 }
402
403 r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops);
404 if (r < 0)
405 return log_oom();
406
407 r = set_put(d->negative_by_name, domain);
408 if (r < 0)
409 return log_oom();
410 if (r > 0)
411 domain = NULL;
412
413 return 0;
414 }
415
416 static int dns_trust_anchor_load_files(
417 DnsTrustAnchor *d,
418 const char *suffix,
419 int (*loader)(DnsTrustAnchor *d, const char *path, unsigned n, const char *line)) {
420
421 _cleanup_strv_free_ char **files = NULL;
422 char **f;
423 int r;
424
425 assert(d);
426 assert(suffix);
427 assert(loader);
428
429 r = conf_files_list_nulstr(&files, suffix, NULL, 0, trust_anchor_dirs);
430 if (r < 0)
431 return log_error_errno(r, "Failed to enumerate %s trust anchor files: %m", suffix);
432
433 STRV_FOREACH(f, files) {
434 _cleanup_fclose_ FILE *g = NULL;
435 char line[LINE_MAX];
436 unsigned n = 0;
437
438 g = fopen(*f, "r");
439 if (!g) {
440 if (errno == ENOENT)
441 continue;
442
443 log_warning_errno(errno, "Failed to open %s: %m", *f);
444 continue;
445 }
446
447 FOREACH_LINE(line, g, log_warning_errno(errno, "Failed to read %s, ignoring: %m", *f)) {
448 char *l;
449
450 n++;
451
452 l = strstrip(line);
453 if (isempty(l))
454 continue;
455
456 if (*l == ';')
457 continue;
458
459 (void) loader(d, *f, n, l);
460 }
461 }
462
463 return 0;
464 }
465
466 static int domain_name_cmp(const void *a, const void *b) {
467 char **x = (char**) a, **y = (char**) b;
468
469 return dns_name_compare_func(*x, *y);
470 }
471
472 static int dns_trust_anchor_dump(DnsTrustAnchor *d) {
473 DnsAnswer *a;
474 Iterator i;
475
476 assert(d);
477
478 if (hashmap_isempty(d->positive_by_key))
479 log_info("No positive trust anchors defined.");
480 else {
481 log_info("Positive Trust Anchors:");
482 HASHMAP_FOREACH(a, d->positive_by_key, i) {
483 DnsResourceRecord *rr;
484
485 DNS_ANSWER_FOREACH(rr, a)
486 log_info("%s", dns_resource_record_to_string(rr));
487 }
488 }
489
490 if (set_isempty(d->negative_by_name))
491 log_info("No negative trust anchors defined.");
492 else {
493 _cleanup_free_ char **l = NULL, *j = NULL;
494
495 l = set_get_strv(d->negative_by_name);
496 if (!l)
497 return log_oom();
498
499 qsort_safe(l, set_size(d->negative_by_name), sizeof(char*), domain_name_cmp);
500
501 j = strv_join(l, " ");
502 if (!j)
503 return log_oom();
504
505 log_info("Negative trust anchors: %s", j);
506 }
507
508 return 0;
509 }
510
511 int dns_trust_anchor_load(DnsTrustAnchor *d) {
512 int r;
513
514 assert(d);
515
516 /* If loading things from disk fails, we don't consider this fatal */
517 (void) dns_trust_anchor_load_files(d, ".positive", dns_trust_anchor_load_positive);
518 (void) dns_trust_anchor_load_files(d, ".negative", dns_trust_anchor_load_negative);
519
520 /* However, if the built-in DS fails, then we have a problem. */
521 r = dns_trust_anchor_add_builtin_positive(d);
522 if (r < 0)
523 return log_error_errno(r, "Failed to add built-in positive trust anchor: %m");
524
525 r = dns_trust_anchor_add_builtin_negative(d);
526 if (r < 0)
527 return log_error_errno(r, "Failed to add built-in negative trust anchor: %m");
528
529 dns_trust_anchor_dump(d);
530
531 return 0;
532 }
533
534 void dns_trust_anchor_flush(DnsTrustAnchor *d) {
535 assert(d);
536
537 d->positive_by_key = hashmap_free_with_destructor(d->positive_by_key, dns_answer_unref);
538 d->revoked_by_rr = set_free_with_destructor(d->revoked_by_rr, dns_resource_record_unref);
539 d->negative_by_name = set_free_free(d->negative_by_name);
540 }
541
542 int dns_trust_anchor_lookup_positive(DnsTrustAnchor *d, const DnsResourceKey *key, DnsAnswer **ret) {
543 DnsAnswer *a;
544
545 assert(d);
546 assert(key);
547 assert(ret);
548
549 /* We only serve DS and DNSKEY RRs. */
550 if (!IN_SET(key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
551 return 0;
552
553 a = hashmap_get(d->positive_by_key, key);
554 if (!a)
555 return 0;
556
557 *ret = dns_answer_ref(a);
558 return 1;
559 }
560
561 int dns_trust_anchor_lookup_negative(DnsTrustAnchor *d, const char *name) {
562 int r;
563
564 assert(d);
565 assert(name);
566
567 for (;;) {
568 /* If the domain is listed as-is in the NTA database, then that counts */
569 if (set_contains(d->negative_by_name, name))
570 return true;
571
572 /* If the domain isn't listed as NTA, but is listed as positive trust anchor, then that counts. See RFC
573 * 7646, section 1.1 */
574 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DS, name)))
575 return false;
576
577 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_KEY, name)))
578 return false;
579
580 /* And now, let's look at the parent, and check that too */
581 r = dns_name_parent(&name);
582 if (r < 0)
583 return r;
584 if (r == 0)
585 break;
586 }
587
588 return false;
589 }
590
591 static int dns_trust_anchor_revoked_put(DnsTrustAnchor *d, DnsResourceRecord *rr) {
592 int r;
593
594 assert(d);
595
596 r = set_ensure_allocated(&d->revoked_by_rr, &dns_resource_record_hash_ops);
597 if (r < 0)
598 return r;
599
600 r = set_put(d->revoked_by_rr, rr);
601 if (r < 0)
602 return r;
603 if (r > 0)
604 dns_resource_record_ref(rr);
605
606 return r;
607 }
608
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 int r;
613
614 /* Remember that this is a revoked trust anchor RR */
615 r = dns_trust_anchor_revoked_put(d, rr);
616 if (r < 0)
617 return r;
618
619 /* Remove this from the positive trust anchor */
620 old_answer = hashmap_get(d->positive_by_key, rr->key);
621 if (!old_answer)
622 return 0;
623
624 new_answer = dns_answer_ref(old_answer);
625
626 r = dns_answer_remove_by_rr(&new_answer, rr);
627 if (r <= 0)
628 return r;
629
630 /* We found the key! Warn the user */
631 log_struct(LOG_WARNING,
632 "MESSAGE_ID=" SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED_STR,
633 LOG_MESSAGE("DNSSEC trust anchor %s has been revoked.\n"
634 "Please update the trust anchor, or upgrade your operating system.",
635 strna(dns_resource_record_to_string(rr))),
636 "TRUST_ANCHOR=%s", dns_resource_record_to_string(rr));
637
638 if (dns_answer_size(new_answer) <= 0) {
639 assert_se(hashmap_remove(d->positive_by_key, rr->key) == old_answer);
640 dns_answer_unref(old_answer);
641 return 1;
642 }
643
644 r = hashmap_replace(d->positive_by_key, new_answer->items[0].rr->key, new_answer);
645 if (r < 0)
646 return r;
647
648 new_answer = NULL;
649 dns_answer_unref(old_answer);
650 return 1;
651 }
652
653 static int dns_trust_anchor_check_revoked_one(DnsTrustAnchor *d, DnsResourceRecord *revoked_dnskey) {
654 DnsAnswer *a;
655 int r;
656
657 assert(d);
658 assert(revoked_dnskey);
659 assert(revoked_dnskey->key->type == DNS_TYPE_DNSKEY);
660 assert(revoked_dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE);
661
662 a = hashmap_get(d->positive_by_key, revoked_dnskey->key);
663 if (a) {
664 DnsResourceRecord *anchor;
665
666 /* First, look for the precise DNSKEY in our trust anchor database */
667
668 DNS_ANSWER_FOREACH(anchor, a) {
669
670 if (anchor->dnskey.protocol != revoked_dnskey->dnskey.protocol)
671 continue;
672
673 if (anchor->dnskey.algorithm != revoked_dnskey->dnskey.algorithm)
674 continue;
675
676 if (anchor->dnskey.key_size != revoked_dnskey->dnskey.key_size)
677 continue;
678
679 /* Note that we allow the REVOKE bit to be
680 * different! It will be set in the revoked
681 * key, but unset in our version of it */
682 if (((anchor->dnskey.flags ^ revoked_dnskey->dnskey.flags) | DNSKEY_FLAG_REVOKE) != DNSKEY_FLAG_REVOKE)
683 continue;
684
685 if (memcmp(anchor->dnskey.key, revoked_dnskey->dnskey.key, anchor->dnskey.key_size) != 0)
686 continue;
687
688 dns_trust_anchor_remove_revoked(d, anchor);
689 break;
690 }
691 }
692
693 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)));
694 if (a) {
695 DnsResourceRecord *anchor;
696
697 /* Second, look for DS RRs matching this DNSKEY in our trust anchor database */
698
699 DNS_ANSWER_FOREACH(anchor, a) {
700
701 /* We set mask_revoke to true here, since our
702 * DS fingerprint will be the one of the
703 * unrevoked DNSKEY, but the one we got passed
704 * here has the bit set. */
705 r = dnssec_verify_dnskey_by_ds(revoked_dnskey, anchor, true);
706 if (r < 0)
707 return r;
708 if (r == 0)
709 continue;
710
711 dns_trust_anchor_remove_revoked(d, anchor);
712 break;
713 }
714 }
715
716 return 0;
717 }
718
719 int dns_trust_anchor_check_revoked(DnsTrustAnchor *d, DnsResourceRecord *dnskey, DnsAnswer *rrs) {
720 DnsResourceRecord *rrsig;
721 int r;
722
723 assert(d);
724 assert(dnskey);
725
726 /* Looks if "dnskey" is a self-signed RR that has been revoked
727 * and matches one of our trust anchor entries. If so, removes
728 * it from the trust anchor and returns > 0. */
729
730 if (dnskey->key->type != DNS_TYPE_DNSKEY)
731 return 0;
732
733 /* Is this DNSKEY revoked? */
734 if ((dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE) == 0)
735 return 0;
736
737 /* Could this be interesting to us at all? If not,
738 * there's no point in looking for and verifying a
739 * self-signed RRSIG. */
740 if (!dns_trust_anchor_knows_domain_positive(d, dns_resource_key_name(dnskey->key)))
741 return 0;
742
743 /* Look for a self-signed RRSIG in the other rrs belonging to this DNSKEY */
744 DNS_ANSWER_FOREACH(rrsig, rrs) {
745 DnssecResult result;
746
747 if (rrsig->key->type != DNS_TYPE_RRSIG)
748 continue;
749
750 r = dnssec_rrsig_match_dnskey(rrsig, dnskey, true);
751 if (r < 0)
752 return r;
753 if (r == 0)
754 continue;
755
756 r = dnssec_verify_rrset(rrs, dnskey->key, rrsig, dnskey, USEC_INFINITY, &result);
757 if (r < 0)
758 return r;
759 if (result != DNSSEC_VALIDATED)
760 continue;
761
762 /* Bingo! This is a revoked self-signed DNSKEY. Let's
763 * see if this precise one exists in our trust anchor
764 * database, too. */
765 r = dns_trust_anchor_check_revoked_one(d, dnskey);
766 if (r < 0)
767 return r;
768
769 return 1;
770 }
771
772 return 0;
773 }
774
775 int dns_trust_anchor_is_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) {
776 assert(d);
777
778 if (!IN_SET(rr->key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
779 return 0;
780
781 return set_contains(d->revoked_by_rr, rr);
782 }