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