]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/resolve/resolved-dns-trust-anchor.c
Merge pull request #6428 from boucman/device_reload
[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 first DS RR from https://data.iana.org/root-anchors/root-anchors.xml, retrieved December 2015 */
39 static const uint8_t root_digest1[] =
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 /* The second DS RR from https://data.iana.org/root-anchors/root-anchors.xml, retrieved February 2017 */
44 static const uint8_t root_digest2[] =
45 { 0xE0, 0x6D, 0x44, 0xB8, 0x0B, 0x8F, 0x1D, 0x39, 0xA9, 0x5C, 0x0B, 0x0D, 0x7C, 0x65, 0xD0, 0x84,
46 0x58, 0xE8, 0x80, 0x40, 0x9B, 0xBC, 0x68, 0x34, 0x57, 0x10, 0x42, 0x37, 0xC7, 0xF8, 0xEC, 0x8D };
47
48 static bool dns_trust_anchor_knows_domain_positive(DnsTrustAnchor *d, const char *name) {
49 assert(d);
50
51 /* Returns true if there's an entry for the specified domain
52 * name in our trust anchor */
53
54 return
55 hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DNSKEY, name)) ||
56 hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DS, name));
57 }
58
59 static int add_root_ksk(
60 DnsAnswer *answer,
61 DnsResourceKey *key,
62 uint16_t key_tag,
63 uint8_t algorithm,
64 uint8_t digest_type,
65 const void *digest,
66 size_t digest_size) {
67
68 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
69 int r;
70
71 rr = dns_resource_record_new(key);
72 if (!rr)
73 return -ENOMEM;
74
75 rr->ds.key_tag = key_tag;
76 rr->ds.algorithm = algorithm;
77 rr->ds.digest_type = digest_type;
78 rr->ds.digest_size = digest_size;
79 rr->ds.digest = memdup(digest, rr->ds.digest_size);
80 if (!rr->ds.digest)
81 return -ENOMEM;
82
83 r = dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
84 if (r < 0)
85 return r;
86
87 return 0;
88 }
89
90 static int dns_trust_anchor_add_builtin_positive(DnsTrustAnchor *d) {
91 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
92 _cleanup_(dns_resource_key_unrefp) DnsResourceKey *key = NULL;
93 int r;
94
95 assert(d);
96
97 r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
98 if (r < 0)
99 return r;
100
101 /* Only add the built-in trust anchor if there's neither a DS nor a DNSKEY defined for the root domain. That
102 * way users have an easy way to override the root domain DS/DNSKEY data. */
103 if (dns_trust_anchor_knows_domain_positive(d, "."))
104 return 0;
105
106 key = dns_resource_key_new(DNS_CLASS_IN, DNS_TYPE_DS, "");
107 if (!key)
108 return -ENOMEM;
109
110 answer = dns_answer_new(2);
111 if (!answer)
112 return -ENOMEM;
113
114 /* Add the two RRs from https://data.iana.org/root-anchors/root-anchors.xml */
115 r = add_root_ksk(answer, key, 19036, DNSSEC_ALGORITHM_RSASHA256, DNSSEC_DIGEST_SHA256, root_digest1, sizeof(root_digest1));
116 if (r < 0)
117 return r;
118
119 r = add_root_ksk(answer, key, 20326, DNSSEC_ALGORITHM_RSASHA256, DNSSEC_DIGEST_SHA256, root_digest2, sizeof(root_digest2));
120 if (r < 0)
121 return r;
122
123 r = hashmap_put(d->positive_by_key, key, answer);
124 if (r < 0)
125 return r;
126
127 answer = NULL;
128 return 0;
129 }
130
131 static int dns_trust_anchor_add_builtin_negative(DnsTrustAnchor *d) {
132
133 static const char private_domains[] =
134 /* RFC 6761 says that .test is a special domain for
135 * testing and not to be installed in the root zone */
136 "test\0"
137
138 /* RFC 6761 says that these reverse IP lookup ranges
139 * are for private addresses, and hence should not
140 * show up in the root zone */
141 "10.in-addr.arpa\0"
142 "16.172.in-addr.arpa\0"
143 "17.172.in-addr.arpa\0"
144 "18.172.in-addr.arpa\0"
145 "19.172.in-addr.arpa\0"
146 "20.172.in-addr.arpa\0"
147 "21.172.in-addr.arpa\0"
148 "22.172.in-addr.arpa\0"
149 "23.172.in-addr.arpa\0"
150 "24.172.in-addr.arpa\0"
151 "25.172.in-addr.arpa\0"
152 "26.172.in-addr.arpa\0"
153 "27.172.in-addr.arpa\0"
154 "28.172.in-addr.arpa\0"
155 "29.172.in-addr.arpa\0"
156 "30.172.in-addr.arpa\0"
157 "31.172.in-addr.arpa\0"
158 "168.192.in-addr.arpa\0"
159
160 /* The same, but for IPv6. */
161 "d.f.ip6.arpa\0"
162
163 /* RFC 6762 reserves the .local domain for Multicast
164 * DNS, it hence cannot appear in the root zone. (Note
165 * that we by default do not route .local traffic to
166 * DNS anyway, except when a configured search domain
167 * suggests so.) */
168 "local\0"
169
170 /* These two are well known, popular private zone
171 * TLDs, that are blocked from delegation, according
172 * to:
173 * http://icannwiki.com/Name_Collision#NGPC_Resolution
174 *
175 * There's also ongoing work on making this official
176 * in an RRC:
177 * https://www.ietf.org/archive/id/draft-chapin-additional-reserved-tlds-02.txt */
178 "home\0"
179 "corp\0"
180
181 /* The following four TLDs are suggested for private
182 * zones in RFC 6762, Appendix G, and are hence very
183 * unlikely to be made official TLDs any day soon */
184 "lan\0"
185 "intranet\0"
186 "internal\0"
187 "private\0";
188
189 const char *name;
190 int r;
191
192 assert(d);
193
194 /* Only add the built-in trust anchor if there's no negative
195 * trust anchor defined at all. This enables easy overriding
196 * of negative trust anchors. */
197
198 if (set_size(d->negative_by_name) > 0)
199 return 0;
200
201 r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops);
202 if (r < 0)
203 return r;
204
205 /* We add a couple of domains as default negative trust
206 * anchors, where it's very unlikely they will be installed in
207 * the root zone. If they exist they must be private, and thus
208 * unsigned. */
209
210 NULSTR_FOREACH(name, private_domains) {
211
212 if (dns_trust_anchor_knows_domain_positive(d, name))
213 continue;
214
215 r = set_put_strdup(d->negative_by_name, name);
216 if (r < 0)
217 return r;
218 }
219
220 return 0;
221 }
222
223 static int dns_trust_anchor_load_positive(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
224 _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL;
225 _cleanup_free_ char *domain = NULL, *class = NULL, *type = NULL;
226 _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL;
227 DnsAnswer *old_answer = NULL;
228 const char *p = s;
229 int r;
230
231 assert(d);
232 assert(line);
233
234 r = extract_first_word(&p, &domain, NULL, EXTRACT_QUOTES);
235 if (r < 0)
236 return log_warning_errno(r, "Unable to parse domain in line %s:%u: %m", path, line);
237
238 if (!dns_name_is_valid(domain)) {
239 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
240 return -EINVAL;
241 }
242
243 r = extract_many_words(&p, NULL, 0, &class, &type, NULL);
244 if (r < 0)
245 return log_warning_errno(r, "Unable to parse class and type in line %s:%u: %m", path, line);
246 if (r != 2) {
247 log_warning("Missing class or type in line %s:%u", path, line);
248 return -EINVAL;
249 }
250
251 if (!strcaseeq(class, "IN")) {
252 log_warning("RR class %s is not supported, ignoring line %s:%u.", class, path, line);
253 return -EINVAL;
254 }
255
256 if (strcaseeq(type, "DS")) {
257 _cleanup_free_ char *key_tag = NULL, *algorithm = NULL, *digest_type = NULL, *digest = NULL;
258 _cleanup_free_ void *dd = NULL;
259 uint16_t kt;
260 int a, dt;
261 size_t l;
262
263 r = extract_many_words(&p, NULL, 0, &key_tag, &algorithm, &digest_type, &digest, NULL);
264 if (r < 0) {
265 log_warning_errno(r, "Failed to parse DS parameters on line %s:%u: %m", path, line);
266 return -EINVAL;
267 }
268 if (r != 4) {
269 log_warning("Missing DS parameters on line %s:%u", path, line);
270 return -EINVAL;
271 }
272
273 r = safe_atou16(key_tag, &kt);
274 if (r < 0)
275 return log_warning_errno(r, "Failed to parse DS key tag %s on line %s:%u: %m", key_tag, path, line);
276
277 a = dnssec_algorithm_from_string(algorithm);
278 if (a < 0) {
279 log_warning("Failed to parse DS algorithm %s on line %s:%u", algorithm, path, line);
280 return -EINVAL;
281 }
282
283 dt = dnssec_digest_from_string(digest_type);
284 if (dt < 0) {
285 log_warning("Failed to parse DS digest type %s on line %s:%u", digest_type, path, line);
286 return -EINVAL;
287 }
288
289 r = unhexmem(digest, strlen(digest), &dd, &l);
290 if (r < 0) {
291 log_warning("Failed to parse DS digest %s on line %s:%u", digest, path, line);
292 return -EINVAL;
293 }
294
295 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, domain);
296 if (!rr)
297 return log_oom();
298
299 rr->ds.key_tag = kt;
300 rr->ds.algorithm = a;
301 rr->ds.digest_type = dt;
302 rr->ds.digest_size = l;
303 rr->ds.digest = dd;
304 dd = NULL;
305
306 } else if (strcaseeq(type, "DNSKEY")) {
307 _cleanup_free_ char *flags = NULL, *protocol = NULL, *algorithm = NULL, *key = NULL;
308 _cleanup_free_ void *k = NULL;
309 uint16_t f;
310 size_t l;
311 int a;
312
313 r = extract_many_words(&p, NULL, 0, &flags, &protocol, &algorithm, &key, NULL);
314 if (r < 0)
315 return log_warning_errno(r, "Failed to parse DNSKEY parameters on line %s:%u: %m", path, line);
316 if (r != 4) {
317 log_warning("Missing DNSKEY parameters on line %s:%u", path, line);
318 return -EINVAL;
319 }
320
321 if (!streq(protocol, "3")) {
322 log_warning("DNSKEY Protocol is not 3 on line %s:%u", path, line);
323 return -EINVAL;
324 }
325
326 r = safe_atou16(flags, &f);
327 if (r < 0)
328 return log_warning_errno(r, "Failed to parse DNSKEY flags field %s on line %s:%u", flags, path, line);
329 if ((f & DNSKEY_FLAG_ZONE_KEY) == 0) {
330 log_warning("DNSKEY lacks zone key bit set on line %s:%u", path, line);
331 return -EINVAL;
332 }
333 if ((f & DNSKEY_FLAG_REVOKE)) {
334 log_warning("DNSKEY is already revoked on line %s:%u", path, line);
335 return -EINVAL;
336 }
337
338 a = dnssec_algorithm_from_string(algorithm);
339 if (a < 0) {
340 log_warning("Failed to parse DNSKEY algorithm %s on line %s:%u", algorithm, path, line);
341 return -EINVAL;
342 }
343
344 r = unbase64mem(key, strlen(key), &k, &l);
345 if (r < 0)
346 return log_warning_errno(r, "Failed to parse DNSKEY key data %s on line %s:%u", key, path, line);
347
348 rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, domain);
349 if (!rr)
350 return log_oom();
351
352 rr->dnskey.flags = f;
353 rr->dnskey.protocol = 3;
354 rr->dnskey.algorithm = a;
355 rr->dnskey.key_size = l;
356 rr->dnskey.key = k;
357 k = NULL;
358
359 } else {
360 log_warning("RR type %s is not supported, ignoring line %s:%u.", type, path, line);
361 return -EINVAL;
362 }
363
364 if (!isempty(p)) {
365 log_warning("Trailing garbage on line %s:%u, ignoring line.", path, line);
366 return -EINVAL;
367 }
368
369 r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops);
370 if (r < 0)
371 return log_oom();
372
373 old_answer = hashmap_get(d->positive_by_key, rr->key);
374 answer = dns_answer_ref(old_answer);
375
376 r = dns_answer_add_extend(&answer, rr, 0, DNS_ANSWER_AUTHENTICATED);
377 if (r < 0)
378 return log_error_errno(r, "Failed to add trust anchor RR: %m");
379
380 r = hashmap_replace(d->positive_by_key, rr->key, answer);
381 if (r < 0)
382 return log_error_errno(r, "Failed to add answer to trust anchor: %m");
383
384 old_answer = dns_answer_unref(old_answer);
385 answer = NULL;
386
387 return 0;
388 }
389
390 static int dns_trust_anchor_load_negative(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) {
391 _cleanup_free_ char *domain = NULL;
392 const char *p = s;
393 int r;
394
395 assert(d);
396 assert(line);
397
398 r = extract_first_word(&p, &domain, NULL, EXTRACT_QUOTES);
399 if (r < 0)
400 return log_warning_errno(r, "Unable to parse line %s:%u: %m", path, line);
401
402 if (!dns_name_is_valid(domain)) {
403 log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line);
404 return -EINVAL;
405 }
406
407 if (!isempty(p)) {
408 log_warning("Trailing garbage at line %s:%u, ignoring line.", path, line);
409 return -EINVAL;
410 }
411
412 r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops);
413 if (r < 0)
414 return log_oom();
415
416 r = set_put(d->negative_by_name, domain);
417 if (r < 0)
418 return log_oom();
419 if (r > 0)
420 domain = NULL;
421
422 return 0;
423 }
424
425 static int dns_trust_anchor_load_files(
426 DnsTrustAnchor *d,
427 const char *suffix,
428 int (*loader)(DnsTrustAnchor *d, const char *path, unsigned n, const char *line)) {
429
430 _cleanup_strv_free_ char **files = NULL;
431 char **f;
432 int r;
433
434 assert(d);
435 assert(suffix);
436 assert(loader);
437
438 r = conf_files_list_nulstr(&files, suffix, NULL, 0, trust_anchor_dirs);
439 if (r < 0)
440 return log_error_errno(r, "Failed to enumerate %s trust anchor files: %m", suffix);
441
442 STRV_FOREACH(f, files) {
443 _cleanup_fclose_ FILE *g = NULL;
444 char line[LINE_MAX];
445 unsigned n = 0;
446
447 g = fopen(*f, "r");
448 if (!g) {
449 if (errno == ENOENT)
450 continue;
451
452 log_warning_errno(errno, "Failed to open %s: %m", *f);
453 continue;
454 }
455
456 FOREACH_LINE(line, g, log_warning_errno(errno, "Failed to read %s, ignoring: %m", *f)) {
457 char *l;
458
459 n++;
460
461 l = strstrip(line);
462 if (isempty(l))
463 continue;
464
465 if (*l == ';')
466 continue;
467
468 (void) loader(d, *f, n, l);
469 }
470 }
471
472 return 0;
473 }
474
475 static int domain_name_cmp(const void *a, const void *b) {
476 char **x = (char**) a, **y = (char**) b;
477
478 return dns_name_compare_func(*x, *y);
479 }
480
481 static int dns_trust_anchor_dump(DnsTrustAnchor *d) {
482 DnsAnswer *a;
483 Iterator i;
484
485 assert(d);
486
487 if (hashmap_isempty(d->positive_by_key))
488 log_info("No positive trust anchors defined.");
489 else {
490 log_info("Positive Trust Anchors:");
491 HASHMAP_FOREACH(a, d->positive_by_key, i) {
492 DnsResourceRecord *rr;
493
494 DNS_ANSWER_FOREACH(rr, a)
495 log_info("%s", dns_resource_record_to_string(rr));
496 }
497 }
498
499 if (set_isempty(d->negative_by_name))
500 log_info("No negative trust anchors defined.");
501 else {
502 _cleanup_free_ char **l = NULL, *j = NULL;
503
504 l = set_get_strv(d->negative_by_name);
505 if (!l)
506 return log_oom();
507
508 qsort_safe(l, set_size(d->negative_by_name), sizeof(char*), domain_name_cmp);
509
510 j = strv_join(l, " ");
511 if (!j)
512 return log_oom();
513
514 log_info("Negative trust anchors: %s", j);
515 }
516
517 return 0;
518 }
519
520 int dns_trust_anchor_load(DnsTrustAnchor *d) {
521 int r;
522
523 assert(d);
524
525 /* If loading things from disk fails, we don't consider this fatal */
526 (void) dns_trust_anchor_load_files(d, ".positive", dns_trust_anchor_load_positive);
527 (void) dns_trust_anchor_load_files(d, ".negative", dns_trust_anchor_load_negative);
528
529 /* However, if the built-in DS fails, then we have a problem. */
530 r = dns_trust_anchor_add_builtin_positive(d);
531 if (r < 0)
532 return log_error_errno(r, "Failed to add built-in positive trust anchor: %m");
533
534 r = dns_trust_anchor_add_builtin_negative(d);
535 if (r < 0)
536 return log_error_errno(r, "Failed to add built-in negative trust anchor: %m");
537
538 dns_trust_anchor_dump(d);
539
540 return 0;
541 }
542
543 void dns_trust_anchor_flush(DnsTrustAnchor *d) {
544 DnsAnswer *a;
545 DnsResourceRecord *rr;
546
547 assert(d);
548
549 while ((a = hashmap_steal_first(d->positive_by_key)))
550 dns_answer_unref(a);
551 d->positive_by_key = hashmap_free(d->positive_by_key);
552
553 while ((rr = set_steal_first(d->revoked_by_rr)))
554 dns_resource_record_unref(rr);
555 d->revoked_by_rr = set_free(d->revoked_by_rr);
556
557 d->negative_by_name = set_free_free(d->negative_by_name);
558 }
559
560 int dns_trust_anchor_lookup_positive(DnsTrustAnchor *d, const DnsResourceKey *key, DnsAnswer **ret) {
561 DnsAnswer *a;
562
563 assert(d);
564 assert(key);
565 assert(ret);
566
567 /* We only serve DS and DNSKEY RRs. */
568 if (!IN_SET(key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
569 return 0;
570
571 a = hashmap_get(d->positive_by_key, key);
572 if (!a)
573 return 0;
574
575 *ret = dns_answer_ref(a);
576 return 1;
577 }
578
579 int dns_trust_anchor_lookup_negative(DnsTrustAnchor *d, const char *name) {
580 int r;
581
582 assert(d);
583 assert(name);
584
585 for (;;) {
586 /* If the domain is listed as-is in the NTA database, then that counts */
587 if (set_contains(d->negative_by_name, name))
588 return true;
589
590 /* If the domain isn't listed as NTA, but is listed as positive trust anchor, then that counts. See RFC
591 * 7646, section 1.1 */
592 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_DS, name)))
593 return false;
594
595 if (hashmap_contains(d->positive_by_key, &DNS_RESOURCE_KEY_CONST(DNS_CLASS_IN, DNS_TYPE_KEY, name)))
596 return false;
597
598 /* And now, let's look at the parent, and check that too */
599 r = dns_name_parent(&name);
600 if (r < 0)
601 return r;
602 if (r == 0)
603 break;
604 }
605
606 return false;
607 }
608
609 static int dns_trust_anchor_revoked_put(DnsTrustAnchor *d, DnsResourceRecord *rr) {
610 int r;
611
612 assert(d);
613
614 r = set_ensure_allocated(&d->revoked_by_rr, &dns_resource_record_hash_ops);
615 if (r < 0)
616 return r;
617
618 r = set_put(d->revoked_by_rr, rr);
619 if (r < 0)
620 return r;
621 if (r > 0)
622 dns_resource_record_ref(rr);
623
624 return r;
625 }
626
627 static int dns_trust_anchor_remove_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) {
628 _cleanup_(dns_answer_unrefp) DnsAnswer *new_answer = NULL;
629 DnsAnswer *old_answer;
630 int r;
631
632 /* Remember that this is a revoked trust anchor RR */
633 r = dns_trust_anchor_revoked_put(d, rr);
634 if (r < 0)
635 return r;
636
637 /* Remove this from the positive trust anchor */
638 old_answer = hashmap_get(d->positive_by_key, rr->key);
639 if (!old_answer)
640 return 0;
641
642 new_answer = dns_answer_ref(old_answer);
643
644 r = dns_answer_remove_by_rr(&new_answer, rr);
645 if (r <= 0)
646 return r;
647
648 /* We found the key! Warn the user */
649 log_struct(LOG_WARNING,
650 "MESSAGE_ID=" SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED_STR,
651 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)),
652 "TRUST_ANCHOR=%s", dns_resource_record_to_string(rr),
653 NULL);
654
655 if (dns_answer_size(new_answer) <= 0) {
656 assert_se(hashmap_remove(d->positive_by_key, rr->key) == old_answer);
657 dns_answer_unref(old_answer);
658 return 1;
659 }
660
661 r = hashmap_replace(d->positive_by_key, new_answer->items[0].rr->key, new_answer);
662 if (r < 0)
663 return r;
664
665 new_answer = NULL;
666 dns_answer_unref(old_answer);
667 return 1;
668 }
669
670 static int dns_trust_anchor_check_revoked_one(DnsTrustAnchor *d, DnsResourceRecord *revoked_dnskey) {
671 DnsAnswer *a;
672 int r;
673
674 assert(d);
675 assert(revoked_dnskey);
676 assert(revoked_dnskey->key->type == DNS_TYPE_DNSKEY);
677 assert(revoked_dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE);
678
679 a = hashmap_get(d->positive_by_key, revoked_dnskey->key);
680 if (a) {
681 DnsResourceRecord *anchor;
682
683 /* First, look for the precise DNSKEY in our trust anchor database */
684
685 DNS_ANSWER_FOREACH(anchor, a) {
686
687 if (anchor->dnskey.protocol != revoked_dnskey->dnskey.protocol)
688 continue;
689
690 if (anchor->dnskey.algorithm != revoked_dnskey->dnskey.algorithm)
691 continue;
692
693 if (anchor->dnskey.key_size != revoked_dnskey->dnskey.key_size)
694 continue;
695
696 /* Note that we allow the REVOKE bit to be
697 * different! It will be set in the revoked
698 * key, but unset in our version of it */
699 if (((anchor->dnskey.flags ^ revoked_dnskey->dnskey.flags) | DNSKEY_FLAG_REVOKE) != DNSKEY_FLAG_REVOKE)
700 continue;
701
702 if (memcmp(anchor->dnskey.key, revoked_dnskey->dnskey.key, anchor->dnskey.key_size) != 0)
703 continue;
704
705 dns_trust_anchor_remove_revoked(d, anchor);
706 break;
707 }
708 }
709
710 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)));
711 if (a) {
712 DnsResourceRecord *anchor;
713
714 /* Second, look for DS RRs matching this DNSKEY in our trust anchor database */
715
716 DNS_ANSWER_FOREACH(anchor, a) {
717
718 /* We set mask_revoke to true here, since our
719 * DS fingerprint will be the one of the
720 * unrevoked DNSKEY, but the one we got passed
721 * here has the bit set. */
722 r = dnssec_verify_dnskey_by_ds(revoked_dnskey, anchor, true);
723 if (r < 0)
724 return r;
725 if (r == 0)
726 continue;
727
728 dns_trust_anchor_remove_revoked(d, anchor);
729 break;
730 }
731 }
732
733 return 0;
734 }
735
736 int dns_trust_anchor_check_revoked(DnsTrustAnchor *d, DnsResourceRecord *dnskey, DnsAnswer *rrs) {
737 DnsResourceRecord *rrsig;
738 int r;
739
740 assert(d);
741 assert(dnskey);
742
743 /* Looks if "dnskey" is a self-signed RR that has been revoked
744 * and matches one of our trust anchor entries. If so, removes
745 * it from the trust anchor and returns > 0. */
746
747 if (dnskey->key->type != DNS_TYPE_DNSKEY)
748 return 0;
749
750 /* Is this DNSKEY revoked? */
751 if ((dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE) == 0)
752 return 0;
753
754 /* Could this be interesting to us at all? If not,
755 * there's no point in looking for and verifying a
756 * self-signed RRSIG. */
757 if (!dns_trust_anchor_knows_domain_positive(d, dns_resource_key_name(dnskey->key)))
758 return 0;
759
760 /* Look for a self-signed RRSIG in the other rrs belonging to this DNSKEY */
761 DNS_ANSWER_FOREACH(rrsig, rrs) {
762 DnssecResult result;
763
764 if (rrsig->key->type != DNS_TYPE_RRSIG)
765 continue;
766
767 r = dnssec_rrsig_match_dnskey(rrsig, dnskey, true);
768 if (r < 0)
769 return r;
770 if (r == 0)
771 continue;
772
773 r = dnssec_verify_rrset(rrs, dnskey->key, rrsig, dnskey, USEC_INFINITY, &result);
774 if (r < 0)
775 return r;
776 if (result != DNSSEC_VALIDATED)
777 continue;
778
779 /* Bingo! This is a revoked self-signed DNSKEY. Let's
780 * see if this precise one exists in our trust anchor
781 * database, too. */
782 r = dns_trust_anchor_check_revoked_one(d, dnskey);
783 if (r < 0)
784 return r;
785
786 return 1;
787 }
788
789 return 0;
790 }
791
792 int dns_trust_anchor_is_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) {
793 assert(d);
794
795 if (!IN_SET(rr->key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY))
796 return 0;
797
798 return set_contains(d->revoked_by_rr, rr);
799 }