]>
Commit | Line | Data |
---|---|---|
0d2cd476 LP |
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 | ||
0c857028 LP |
20 | #include <sd-messages.h> |
21 | ||
0d2cd476 | 22 | #include "alloc-util.h" |
8e54f5d9 LP |
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" | |
0d2cd476 | 30 | #include "resolved-dns-trust-anchor.h" |
0c857028 | 31 | #include "resolved-dns-dnssec.h" |
8e54f5d9 LP |
32 | #include "set.h" |
33 | #include "string-util.h" | |
34 | #include "strv.h" | |
0d2cd476 | 35 | |
e7d179ac | 36 | static const char trust_anchor_dirs[] = CONF_PATHS_NULSTR("dnssec-trust-anchors.d"); |
8e54f5d9 LP |
37 | |
38 | /* The DS RR from https://data.iana.org/root-anchors/root-anchors.xml, retrieved December 2015 */ | |
0d2cd476 LP |
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 | ||
86e9cbca LP |
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 | ||
30c77809 | 54 | static int dns_trust_anchor_add_builtin_positive(DnsTrustAnchor *d) { |
0d2cd476 LP |
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 | ||
8e54f5d9 | 61 | r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops); |
0d2cd476 LP |
62 | if (r < 0) |
63 | return r; | |
64 | ||
86e9cbca LP |
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, ".")) | |
d76f90f1 LP |
70 | return 0; |
71 | ||
0d2cd476 LP |
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 | ||
105e1512 | 89 | r = dns_answer_add(answer, rr, 0, DNS_ANSWER_AUTHENTICATED); |
0d2cd476 LP |
90 | if (r < 0) |
91 | return r; | |
92 | ||
8e54f5d9 LP |
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 | ||
30c77809 LP |
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 | /* RFC 6762 reserves the .local domain for Multicast | |
131 | * DNS, it hence cannot appear in the root zone. (Note | |
132 | * that we by default do not route .local traffic to | |
133 | * DNS anyway, except when a configured search domain | |
134 | * suggests so.) */ | |
135 | "local\0" | |
136 | ||
137 | /* These two are well known, popular private zone | |
138 | * TLDs, that are blocked from delegation, according | |
139 | * to: | |
140 | * http://icannwiki.com/Name_Collision#NGPC_Resolution | |
141 | * | |
142 | * There's also ongoing work on making this official | |
143 | * in an RRC: | |
144 | * https://www.ietf.org/archive/id/draft-chapin-additional-reserved-tlds-02.txt */ | |
145 | "home\0" | |
146 | "corp\0" | |
147 | ||
148 | /* The following four TLDs are suggested for private | |
149 | * zones in RFC 6762, Appendix G, and are hence very | |
150 | * unlikely to be made official TLDs any day soon */ | |
151 | "lan\0" | |
152 | "intranet\0" | |
153 | "internal\0" | |
154 | "private\0"; | |
155 | ||
156 | const char *name; | |
157 | int r; | |
158 | ||
159 | assert(d); | |
160 | ||
161 | /* Only add the built-in trust anchor if there's no negative | |
162 | * trust anchor defined at all. This enables easy overriding | |
163 | * of negative trust anchors. */ | |
164 | ||
165 | if (set_size(d->negative_by_name) > 0) | |
166 | return 0; | |
167 | ||
168 | r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops); | |
169 | if (r < 0) | |
170 | return r; | |
171 | ||
172 | /* We add a couple of domains as default negative trust | |
173 | * anchors, where it's very unlikely they will be installed in | |
174 | * the root zone. If they exist they must be private, and thus | |
175 | * unsigned. */ | |
176 | ||
177 | NULSTR_FOREACH(name, private_domains) { | |
178 | ||
179 | if (dns_trust_anchor_knows_domain_positive(d, name)) | |
180 | continue; | |
181 | ||
182 | r = set_put_strdup(d->negative_by_name, name); | |
183 | if (r < 0) | |
184 | return r; | |
185 | } | |
186 | ||
187 | return 0; | |
188 | } | |
189 | ||
8e54f5d9 LP |
190 | static int dns_trust_anchor_load_positive(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) { |
191 | _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr = NULL; | |
192 | _cleanup_free_ char *domain = NULL, *class = NULL, *type = NULL; | |
193 | _cleanup_(dns_answer_unrefp) DnsAnswer *answer = NULL; | |
194 | DnsAnswer *old_answer = NULL; | |
195 | const char *p = s; | |
196 | int r; | |
197 | ||
198 | assert(d); | |
199 | assert(line); | |
200 | ||
201 | r = extract_first_word(&p, &domain, NULL, EXTRACT_QUOTES); | |
202 | if (r < 0) | |
203 | return log_warning_errno(r, "Unable to parse domain in line %s:%u: %m", path, line); | |
204 | ||
205 | if (!dns_name_is_valid(domain)) { | |
206 | log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line); | |
207 | return -EINVAL; | |
208 | } | |
209 | ||
210 | r = extract_many_words(&p, NULL, 0, &class, &type, NULL); | |
211 | if (r < 0) | |
212 | return log_warning_errno(r, "Unable to parse class and type in line %s:%u: %m", path, line); | |
213 | if (r != 2) { | |
214 | log_warning("Missing class or type in line %s:%u", path, line); | |
215 | return -EINVAL; | |
216 | } | |
217 | ||
218 | if (!strcaseeq(class, "IN")) { | |
219 | log_warning("RR class %s is not supported, ignoring line %s:%u.", class, path, line); | |
220 | return -EINVAL; | |
221 | } | |
222 | ||
223 | if (strcaseeq(type, "DS")) { | |
224 | _cleanup_free_ char *key_tag = NULL, *algorithm = NULL, *digest_type = NULL, *digest = NULL; | |
225 | _cleanup_free_ void *dd = NULL; | |
226 | uint16_t kt; | |
227 | int a, dt; | |
228 | size_t l; | |
229 | ||
230 | r = extract_many_words(&p, NULL, 0, &key_tag, &algorithm, &digest_type, &digest, NULL); | |
231 | if (r < 0) { | |
232 | log_warning_errno(r, "Failed to parse DS parameters on line %s:%u: %m", path, line); | |
233 | return -EINVAL; | |
234 | } | |
235 | if (r != 4) { | |
236 | log_warning("Missing DS parameters on line %s:%u", path, line); | |
237 | return -EINVAL; | |
238 | } | |
239 | ||
240 | r = safe_atou16(key_tag, &kt); | |
241 | if (r < 0) | |
242 | return log_warning_errno(r, "Failed to parse DS key tag %s on line %s:%u: %m", key_tag, path, line); | |
243 | ||
244 | a = dnssec_algorithm_from_string(algorithm); | |
245 | if (a < 0) { | |
246 | log_warning("Failed to parse DS algorithm %s on line %s:%u", algorithm, path, line); | |
247 | return -EINVAL; | |
248 | } | |
249 | ||
250 | dt = dnssec_digest_from_string(digest_type); | |
251 | if (dt < 0) { | |
252 | log_warning("Failed to parse DS digest type %s on line %s:%u", digest_type, path, line); | |
253 | return -EINVAL; | |
254 | } | |
255 | ||
256 | r = unhexmem(digest, strlen(digest), &dd, &l); | |
257 | if (r < 0) { | |
258 | log_warning("Failed to parse DS digest %s on line %s:%u", digest, path, line); | |
259 | return -EINVAL; | |
260 | } | |
261 | ||
262 | rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DS, domain); | |
263 | if (!rr) | |
264 | return log_oom(); | |
265 | ||
266 | rr->ds.key_tag = kt; | |
267 | rr->ds.algorithm = a; | |
268 | rr->ds.digest_type = dt; | |
269 | rr->ds.digest_size = l; | |
270 | rr->ds.digest = dd; | |
271 | dd = NULL; | |
272 | ||
273 | } else if (strcaseeq(type, "DNSKEY")) { | |
274 | _cleanup_free_ char *flags = NULL, *protocol = NULL, *algorithm = NULL, *key = NULL; | |
275 | _cleanup_free_ void *k = NULL; | |
276 | uint16_t f; | |
277 | size_t l; | |
278 | int a; | |
279 | ||
280 | r = extract_many_words(&p, NULL, 0, &flags, &protocol, &algorithm, &key, NULL); | |
281 | if (r < 0) | |
282 | return log_warning_errno(r, "Failed to parse DNSKEY parameters on line %s:%u: %m", path, line); | |
283 | if (r != 4) { | |
284 | log_warning("Missing DNSKEY parameters on line %s:%u", path, line); | |
285 | return -EINVAL; | |
286 | } | |
287 | ||
288 | if (!streq(protocol, "3")) { | |
289 | log_warning("DNSKEY Protocol is not 3 on line %s:%u", path, line); | |
290 | return -EINVAL; | |
291 | } | |
292 | ||
293 | r = safe_atou16(flags, &f); | |
294 | if (r < 0) | |
295 | return log_warning_errno(r, "Failed to parse DNSKEY flags field %s on line %s:%u", flags, path, line); | |
2a0d751b LP |
296 | if ((f & DNSKEY_FLAG_ZONE_KEY) == 0) { |
297 | log_warning("DNSKEY lacks zone key bit set on line %s:%u", path, line); | |
298 | return -EINVAL; | |
299 | } | |
300 | if ((f & DNSKEY_FLAG_REVOKE)) { | |
301 | log_warning("DNSKEY is already revoked on line %s:%u", path, line); | |
302 | return -EINVAL; | |
303 | } | |
8e54f5d9 LP |
304 | |
305 | a = dnssec_algorithm_from_string(algorithm); | |
306 | if (a < 0) { | |
307 | log_warning("Failed to parse DNSKEY algorithm %s on line %s:%u", algorithm, path, line); | |
308 | return -EINVAL; | |
309 | } | |
310 | ||
311 | r = unbase64mem(key, strlen(key), &k, &l); | |
312 | if (r < 0) | |
313 | return log_warning_errno(r, "Failed to parse DNSKEY key data %s on line %s:%u", key, path, line); | |
314 | ||
315 | rr = dns_resource_record_new_full(DNS_CLASS_IN, DNS_TYPE_DNSKEY, domain); | |
316 | if (!rr) | |
317 | return log_oom(); | |
318 | ||
319 | rr->dnskey.flags = f; | |
320 | rr->dnskey.protocol = 3; | |
321 | rr->dnskey.algorithm = a; | |
322 | rr->dnskey.key_size = l; | |
323 | rr->dnskey.key = k; | |
324 | k = NULL; | |
325 | ||
326 | } else { | |
327 | log_warning("RR type %s is not supported, ignoring line %s:%u.", type, path, line); | |
328 | return -EINVAL; | |
329 | } | |
330 | ||
331 | if (!isempty(p)) { | |
332 | log_warning("Trailing garbage on line %s:%u, ignoring line.", path, line); | |
333 | return -EINVAL; | |
334 | } | |
335 | ||
336 | r = hashmap_ensure_allocated(&d->positive_by_key, &dns_resource_key_hash_ops); | |
0d2cd476 | 337 | if (r < 0) |
b3331c39 | 338 | return log_oom(); |
0d2cd476 | 339 | |
8e54f5d9 LP |
340 | old_answer = hashmap_get(d->positive_by_key, rr->key); |
341 | answer = dns_answer_ref(old_answer); | |
342 | ||
343 | r = dns_answer_add_extend(&answer, rr, 0, DNS_ANSWER_AUTHENTICATED); | |
344 | if (r < 0) | |
345 | return log_error_errno(r, "Failed to add trust anchor RR: %m"); | |
346 | ||
347 | r = hashmap_replace(d->positive_by_key, rr->key, answer); | |
348 | if (r < 0) | |
349 | return log_error_errno(r, "Failed to add answer to trust anchor: %m"); | |
350 | ||
351 | old_answer = dns_answer_unref(old_answer); | |
0d2cd476 | 352 | answer = NULL; |
8e54f5d9 LP |
353 | |
354 | return 0; | |
355 | } | |
356 | ||
357 | static int dns_trust_anchor_load_negative(DnsTrustAnchor *d, const char *path, unsigned line, const char *s) { | |
358 | _cleanup_free_ char *domain = NULL; | |
359 | const char *p = s; | |
360 | int r; | |
361 | ||
362 | assert(d); | |
363 | assert(line); | |
364 | ||
365 | r = extract_first_word(&p, &domain, NULL, EXTRACT_QUOTES); | |
366 | if (r < 0) | |
367 | return log_warning_errno(r, "Unable to parse line %s:%u: %m", path, line); | |
368 | ||
369 | if (!dns_name_is_valid(domain)) { | |
370 | log_warning("Domain name %s is invalid, at line %s:%u, ignoring line.", domain, path, line); | |
371 | return -EINVAL; | |
372 | } | |
373 | ||
374 | if (!isempty(p)) { | |
375 | log_warning("Trailing garbage at line %s:%u, ignoring line.", path, line); | |
376 | return -EINVAL; | |
377 | } | |
378 | ||
379 | r = set_ensure_allocated(&d->negative_by_name, &dns_name_hash_ops); | |
380 | if (r < 0) | |
b3331c39 | 381 | return log_oom(); |
8e54f5d9 LP |
382 | |
383 | r = set_put(d->negative_by_name, domain); | |
384 | if (r < 0) | |
385 | return log_oom(); | |
386 | if (r > 0) | |
387 | domain = NULL; | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
392 | static int dns_trust_anchor_load_files( | |
393 | DnsTrustAnchor *d, | |
394 | const char *suffix, | |
395 | int (*loader)(DnsTrustAnchor *d, const char *path, unsigned n, const char *line)) { | |
396 | ||
397 | _cleanup_strv_free_ char **files = NULL; | |
398 | char **f; | |
399 | int r; | |
400 | ||
401 | assert(d); | |
402 | assert(suffix); | |
403 | assert(loader); | |
404 | ||
405 | r = conf_files_list_nulstr(&files, suffix, NULL, trust_anchor_dirs); | |
406 | if (r < 0) | |
407 | return log_error_errno(r, "Failed to enumerate %s trust anchor files: %m", suffix); | |
408 | ||
409 | STRV_FOREACH(f, files) { | |
410 | _cleanup_fclose_ FILE *g = NULL; | |
411 | char line[LINE_MAX]; | |
412 | unsigned n = 0; | |
413 | ||
414 | g = fopen(*f, "r"); | |
415 | if (!g) { | |
416 | if (errno == ENOENT) | |
417 | continue; | |
418 | ||
419 | log_warning_errno(errno, "Failed to open %s: %m", *f); | |
420 | continue; | |
421 | } | |
422 | ||
423 | FOREACH_LINE(line, g, log_warning_errno(errno, "Failed to read %s, ignoring: %m", *f)) { | |
424 | char *l; | |
425 | ||
426 | n++; | |
427 | ||
428 | l = strstrip(line); | |
429 | if (isempty(l)) | |
430 | continue; | |
431 | ||
432 | if (*l == ';') | |
433 | continue; | |
434 | ||
435 | (void) loader(d, *f, n, l); | |
436 | } | |
437 | } | |
438 | ||
439 | return 0; | |
440 | } | |
441 | ||
bec69050 LP |
442 | static int domain_name_cmp(const void *a, const void *b) { |
443 | char **x = (char**) a, **y = (char**) b; | |
444 | ||
445 | return dns_name_compare_func(*x, *y); | |
446 | } | |
447 | ||
448 | static int dns_trust_anchor_dump(DnsTrustAnchor *d) { | |
8e54f5d9 LP |
449 | DnsAnswer *a; |
450 | Iterator i; | |
451 | ||
452 | assert(d); | |
453 | ||
105f6c4b LP |
454 | if (hashmap_isempty(d->positive_by_key)) |
455 | log_info("No positive trust anchors defined."); | |
456 | else { | |
457 | log_info("Positive Trust Anchors:"); | |
458 | HASHMAP_FOREACH(a, d->positive_by_key, i) { | |
459 | DnsResourceRecord *rr; | |
460 | ||
461 | DNS_ANSWER_FOREACH(rr, a) | |
462 | log_info("%s", dns_resource_record_to_string(rr)); | |
463 | } | |
8e54f5d9 LP |
464 | } |
465 | ||
105f6c4b LP |
466 | if (set_isempty(d->negative_by_name)) |
467 | log_info("No negative trust anchors defined."); | |
468 | else { | |
bec69050 LP |
469 | _cleanup_free_ char **l = NULL, *j = NULL; |
470 | ||
471 | l = set_get_strv(d->negative_by_name); | |
472 | if (!l) | |
473 | return log_oom(); | |
8e54f5d9 | 474 | |
bec69050 LP |
475 | qsort_safe(l, set_size(d->negative_by_name), sizeof(char*), domain_name_cmp); |
476 | ||
477 | j = strv_join(l, " "); | |
478 | if (!j) | |
479 | return log_oom(); | |
480 | ||
481 | log_info("Negative trust anchors: %s", j); | |
8e54f5d9 | 482 | } |
bec69050 LP |
483 | |
484 | return 0; | |
8e54f5d9 LP |
485 | } |
486 | ||
487 | int dns_trust_anchor_load(DnsTrustAnchor *d) { | |
488 | int r; | |
489 | ||
490 | assert(d); | |
491 | ||
492 | /* If loading things from disk fails, we don't consider this fatal */ | |
493 | (void) dns_trust_anchor_load_files(d, ".positive", dns_trust_anchor_load_positive); | |
494 | (void) dns_trust_anchor_load_files(d, ".negative", dns_trust_anchor_load_negative); | |
495 | ||
496 | /* However, if the built-in DS fails, then we have a problem. */ | |
30c77809 LP |
497 | r = dns_trust_anchor_add_builtin_positive(d); |
498 | if (r < 0) | |
499 | return log_error_errno(r, "Failed to add built-in positive trust anchor: %m"); | |
500 | ||
501 | r = dns_trust_anchor_add_builtin_negative(d); | |
8e54f5d9 | 502 | if (r < 0) |
30c77809 | 503 | return log_error_errno(r, "Failed to add built-in negative trust anchor: %m"); |
8e54f5d9 LP |
504 | |
505 | dns_trust_anchor_dump(d); | |
506 | ||
0d2cd476 LP |
507 | return 0; |
508 | } | |
509 | ||
510 | void dns_trust_anchor_flush(DnsTrustAnchor *d) { | |
511 | DnsAnswer *a; | |
c9c72065 | 512 | DnsResourceRecord *rr; |
0d2cd476 LP |
513 | |
514 | assert(d); | |
515 | ||
8e54f5d9 | 516 | while ((a = hashmap_steal_first(d->positive_by_key))) |
0d2cd476 | 517 | dns_answer_unref(a); |
8e54f5d9 | 518 | d->positive_by_key = hashmap_free(d->positive_by_key); |
c9c72065 LP |
519 | |
520 | while ((rr = set_steal_first(d->revoked_by_rr))) | |
521 | dns_resource_record_unref(rr); | |
522 | d->revoked_by_rr = set_free(d->revoked_by_rr); | |
523 | ||
8e54f5d9 | 524 | d->negative_by_name = set_free_free(d->negative_by_name); |
0d2cd476 LP |
525 | } |
526 | ||
8e54f5d9 | 527 | int dns_trust_anchor_lookup_positive(DnsTrustAnchor *d, const DnsResourceKey *key, DnsAnswer **ret) { |
0d2cd476 LP |
528 | DnsAnswer *a; |
529 | ||
530 | assert(d); | |
531 | assert(key); | |
532 | assert(ret); | |
533 | ||
534 | /* We only serve DS and DNSKEY RRs. */ | |
535 | if (!IN_SET(key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY)) | |
536 | return 0; | |
537 | ||
8e54f5d9 | 538 | a = hashmap_get(d->positive_by_key, key); |
0d2cd476 LP |
539 | if (!a) |
540 | return 0; | |
541 | ||
542 | *ret = dns_answer_ref(a); | |
543 | return 1; | |
544 | } | |
8e54f5d9 LP |
545 | |
546 | int dns_trust_anchor_lookup_negative(DnsTrustAnchor *d, const char *name) { | |
547 | assert(d); | |
548 | assert(name); | |
549 | ||
550 | return set_contains(d->negative_by_name, name); | |
551 | } | |
0c857028 | 552 | |
c9c72065 LP |
553 | static int dns_trust_anchor_revoked_put(DnsTrustAnchor *d, DnsResourceRecord *rr) { |
554 | int r; | |
555 | ||
556 | assert(d); | |
557 | ||
558 | r = set_ensure_allocated(&d->revoked_by_rr, &dns_resource_record_hash_ops); | |
559 | if (r < 0) | |
560 | return r; | |
561 | ||
562 | r = set_put(d->revoked_by_rr, rr); | |
563 | if (r < 0) | |
564 | return r; | |
565 | if (r > 0) | |
566 | dns_resource_record_ref(rr); | |
567 | ||
568 | return r; | |
569 | } | |
570 | ||
0c857028 LP |
571 | static int dns_trust_anchor_remove_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) { |
572 | _cleanup_(dns_answer_unrefp) DnsAnswer *new_answer = NULL; | |
573 | DnsAnswer *old_answer; | |
574 | int r; | |
575 | ||
c9c72065 LP |
576 | /* Remember that this is a revoked trust anchor RR */ |
577 | r = dns_trust_anchor_revoked_put(d, rr); | |
578 | if (r < 0) | |
579 | return r; | |
580 | ||
581 | /* Remove this from the positive trust anchor */ | |
0c857028 LP |
582 | old_answer = hashmap_get(d->positive_by_key, rr->key); |
583 | if (!old_answer) | |
584 | return 0; | |
585 | ||
586 | new_answer = dns_answer_ref(old_answer); | |
587 | ||
588 | r = dns_answer_remove_by_rr(&new_answer, rr); | |
589 | if (r <= 0) | |
590 | return r; | |
591 | ||
592 | /* We found the key! Warn the user */ | |
593 | log_struct(LOG_WARNING, | |
594 | LOG_MESSAGE_ID(SD_MESSAGE_DNSSEC_TRUST_ANCHOR_REVOKED), | |
595 | 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)), | |
596 | "TRUST_ANCHOR=%s", dns_resource_record_to_string(rr), | |
597 | NULL); | |
598 | ||
599 | if (dns_answer_size(new_answer) <= 0) { | |
600 | assert_se(hashmap_remove(d->positive_by_key, rr->key) == old_answer); | |
601 | dns_answer_unref(old_answer); | |
602 | return 1; | |
603 | } | |
604 | ||
605 | r = hashmap_replace(d->positive_by_key, new_answer->items[0].rr->key, new_answer); | |
606 | if (r < 0) | |
607 | return r; | |
608 | ||
609 | new_answer = NULL; | |
610 | dns_answer_unref(old_answer); | |
611 | return 1; | |
612 | } | |
613 | ||
614 | static int dns_trust_anchor_check_revoked_one(DnsTrustAnchor *d, DnsResourceRecord *revoked_dnskey) { | |
615 | DnsAnswer *a; | |
616 | int r; | |
617 | ||
618 | assert(d); | |
619 | assert(revoked_dnskey); | |
620 | assert(revoked_dnskey->key->type == DNS_TYPE_DNSKEY); | |
621 | assert(revoked_dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE); | |
622 | ||
623 | a = hashmap_get(d->positive_by_key, revoked_dnskey->key); | |
624 | if (a) { | |
625 | DnsResourceRecord *anchor; | |
626 | ||
627 | /* First, look for the precise DNSKEY in our trust anchor database */ | |
628 | ||
629 | DNS_ANSWER_FOREACH(anchor, a) { | |
630 | ||
631 | if (anchor->dnskey.protocol != revoked_dnskey->dnskey.protocol) | |
632 | continue; | |
633 | ||
634 | if (anchor->dnskey.algorithm != revoked_dnskey->dnskey.algorithm) | |
635 | continue; | |
636 | ||
637 | if (anchor->dnskey.key_size != revoked_dnskey->dnskey.key_size) | |
638 | continue; | |
639 | ||
c9c72065 LP |
640 | /* Note that we allow the REVOKE bit to be |
641 | * different! It will be set in the revoked | |
642 | * key, but unset in our version of it */ | |
0c857028 LP |
643 | if (((anchor->dnskey.flags ^ revoked_dnskey->dnskey.flags) | DNSKEY_FLAG_REVOKE) != DNSKEY_FLAG_REVOKE) |
644 | continue; | |
645 | ||
646 | if (memcmp(anchor->dnskey.key, revoked_dnskey->dnskey.key, anchor->dnskey.key_size) != 0) | |
647 | continue; | |
648 | ||
649 | dns_trust_anchor_remove_revoked(d, anchor); | |
650 | break; | |
651 | } | |
652 | } | |
653 | ||
1c02e7ba | 654 | 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))); |
0c857028 LP |
655 | if (a) { |
656 | DnsResourceRecord *anchor; | |
657 | ||
658 | /* Second, look for DS RRs matching this DNSKEY in our trust anchor database */ | |
659 | ||
660 | DNS_ANSWER_FOREACH(anchor, a) { | |
661 | ||
c9c72065 LP |
662 | /* We set mask_revoke to true here, since our |
663 | * DS fingerprint will be the one of the | |
664 | * unrevoked DNSKEY, but the one we got passed | |
665 | * here has the bit set. */ | |
96bb7673 | 666 | r = dnssec_verify_dnskey_by_ds(revoked_dnskey, anchor, true); |
0c857028 LP |
667 | if (r < 0) |
668 | return r; | |
669 | if (r == 0) | |
670 | continue; | |
671 | ||
672 | dns_trust_anchor_remove_revoked(d, anchor); | |
673 | break; | |
674 | } | |
675 | } | |
676 | ||
677 | return 0; | |
678 | } | |
679 | ||
d424da2a LP |
680 | int dns_trust_anchor_check_revoked(DnsTrustAnchor *d, DnsResourceRecord *dnskey, DnsAnswer *rrs) { |
681 | DnsResourceRecord *rrsig; | |
0c857028 LP |
682 | int r; |
683 | ||
684 | assert(d); | |
d424da2a LP |
685 | assert(dnskey); |
686 | ||
687 | /* Looks if "dnskey" is a self-signed RR that has been revoked | |
688 | * and matches one of our trust anchor entries. If so, removes | |
689 | * it from the trust anchor and returns > 0. */ | |
0c857028 | 690 | |
d424da2a LP |
691 | if (dnskey->key->type != DNS_TYPE_DNSKEY) |
692 | return 0; | |
0c857028 | 693 | |
d424da2a LP |
694 | /* Is this DNSKEY revoked? */ |
695 | if ((dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE) == 0) | |
0c857028 LP |
696 | return 0; |
697 | ||
d424da2a LP |
698 | /* Could this be interesting to us at all? If not, |
699 | * there's no point in looking for and verifying a | |
700 | * self-signed RRSIG. */ | |
1c02e7ba | 701 | if (!dns_trust_anchor_knows_domain_positive(d, dns_resource_key_name(dnskey->key))) |
d424da2a LP |
702 | return 0; |
703 | ||
704 | /* Look for a self-signed RRSIG in the other rrs belonging to this DNSKEY */ | |
705 | DNS_ANSWER_FOREACH(rrsig, rrs) { | |
0c857028 LP |
706 | DnssecResult result; |
707 | ||
d424da2a LP |
708 | if (rrsig->key->type != DNS_TYPE_RRSIG) |
709 | continue; | |
710 | ||
711 | r = dnssec_rrsig_match_dnskey(rrsig, dnskey, true); | |
0c857028 LP |
712 | if (r < 0) |
713 | return r; | |
714 | if (r == 0) | |
715 | continue; | |
716 | ||
d424da2a LP |
717 | r = dnssec_verify_rrset(rrs, dnskey->key, rrsig, dnskey, USEC_INFINITY, &result); |
718 | if (r < 0) | |
719 | return r; | |
720 | if (result != DNSSEC_VALIDATED) | |
0c857028 LP |
721 | continue; |
722 | ||
d424da2a LP |
723 | /* Bingo! This is a revoked self-signed DNSKEY. Let's |
724 | * see if this precise one exists in our trust anchor | |
725 | * database, too. */ | |
726 | r = dns_trust_anchor_check_revoked_one(d, dnskey); | |
727 | if (r < 0) | |
728 | return r; | |
0c857028 | 729 | |
d424da2a | 730 | return 1; |
0c857028 LP |
731 | } |
732 | ||
733 | return 0; | |
734 | } | |
c9c72065 LP |
735 | |
736 | int dns_trust_anchor_is_revoked(DnsTrustAnchor *d, DnsResourceRecord *rr) { | |
737 | assert(d); | |
738 | ||
739 | if (!IN_SET(rr->key->type, DNS_TYPE_DS, DNS_TYPE_DNSKEY)) | |
740 | return 0; | |
741 | ||
742 | return set_contains(d->revoked_by_rr, rr); | |
743 | } |