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