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