Debian Codegraph @

src/resolve/resolved-dns-dnssec.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "bitmap.h"
5 #include "crypto-util.h"
6 #include "dns-answer.h"
7 #include "dns-domain.h"
8 #include "dns-rr.h"
9 #include "dns-type.h"
10 #include "hexdecoct.h"
11 #include "log.h"
12 #include "memory-util.h"
13 #include "memstream-util.h"
14 #include "resolved-dns-dnssec.h"
15 #include "sort-util.h"
16 #include "string-table.h"
17 #include "string-util.h"
18 #include "time-util.h"
19
20
21 #define VERIFY_RRS_MAX 256
22 #define MAX_KEY_SIZE (32*1024)
23
24 /* Permit a maximum clock skew of 1h 10min. This should be enough to deal with DST confusion */
25 #define SKEW_MAX (1*USEC_PER_HOUR + 10*USEC_PER_MINUTE)
26
27 /* Maximum number of NSEC3 iterations we'll do. RFC5155 says 2500 shall be the maximum useful value, but
28 * RFC9276 § 3.2 says that we should reduce the acceptable iteration count */
29 #define NSEC3_ITERATIONS_MAX 100
30
31 /*
32 * The DNSSEC Chain of trust:
33 *
34 * Normal RRs are protected via RRSIG RRs in combination with DNSKEY RRs, all in the same zone
35 * DNSKEY RRs are either protected like normal RRs, or via a DS from a zone "higher" up the tree
36 * DS RRs are protected like normal RRs
37 *
38 * Example chain:
39 * Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS
40 */
41
42 #if HAVE_OPENSSL
43
44 static int rr_compare(DnsResourceRecord * const *a, DnsResourceRecord * const *b) {
45 const DnsResourceRecord *x = *ASSERT_PTR(a), *y = *ASSERT_PTR(b);
46 size_t m;
47 int r;
48
49 /* Let's order the RRs according to RFC 4034, Section 6.3 */
50
51 assert(x);
52 assert(x->wire_format);
53 assert(y);
54 assert(y->wire_format);
55
56 m = MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(x), DNS_RESOURCE_RECORD_RDATA_SIZE(y));
57
58 r = memcmp(DNS_RESOURCE_RECORD_RDATA(x), DNS_RESOURCE_RECORD_RDATA(y), m);
59 if (r != 0)
60 return r;
61
62 return CMP(DNS_RESOURCE_RECORD_RDATA_SIZE(x), DNS_RESOURCE_RECORD_RDATA_SIZE(y));
63 }
64
65 static int dnssec_rsa_verify_raw(
66 const EVP_MD *hash_algorithm,
67 const void *signature, size_t signature_size,
68 const void *data, size_t data_size,
69 const void *exponent, size_t exponent_size,
70 const void *modulus, size_t modulus_size) {
71 int r;
72
73 DISABLE_WARNING_DEPRECATED_DECLARATIONS;
74 _cleanup_(RSA_freep) RSA *rpubkey = NULL;
75 _cleanup_(EVP_PKEY_freep) EVP_PKEY *epubkey = NULL;
76 _cleanup_(EVP_PKEY_CTX_freep) EVP_PKEY_CTX *ctx = NULL;
77 _cleanup_(BN_freep) BIGNUM *e = NULL, *m = NULL;
78
79 assert(hash_algorithm);
80
81 e = sym_BN_bin2bn(exponent, exponent_size, NULL);
82 if (!e)
83 return -EIO;
84
85 m = sym_BN_bin2bn(modulus, modulus_size, NULL);
86 if (!m)
87 return -EIO;
88
89 rpubkey = sym_RSA_new();
90 if (!rpubkey)
91 return -ENOMEM;
92
93 if (sym_RSA_set0_key(rpubkey, m, e, NULL) <= 0)
94 return -EIO;
95 e = m = NULL;
96
97 if ((size_t) sym_RSA_size(rpubkey) != signature_size)
98 return -EINVAL;
99
100 epubkey = sym_EVP_PKEY_new();
101 if (!epubkey)
102 return -ENOMEM;
103
104 if (sym_EVP_PKEY_assign_RSA(epubkey, sym_RSAPublicKey_dup(rpubkey)) <= 0)
105 return -EIO;
106
107 ctx = sym_EVP_PKEY_CTX_new(epubkey, NULL);
108 if (!ctx)
109 return -ENOMEM;
110
111 if (sym_EVP_PKEY_verify_init(ctx) <= 0)
112 return -EIO;
113
114 if (sym_EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) <= 0)
115 return -EIO;
116
117 if (sym_EVP_PKEY_CTX_set_signature_md(ctx, hash_algorithm) <= 0)
118 return -EIO;
119
120 r = sym_EVP_PKEY_verify(ctx, signature, signature_size, data, data_size);
121 if (r < 0)
122 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
123 "Signature verification failed: 0x%lx", sym_ERR_get_error());
124
125 REENABLE_WARNING;
126 return r;
127 }
128
129 static int dnssec_rsa_verify(
130 const EVP_MD *hash_algorithm,
131 const void *hash, size_t hash_size,
132 DnsResourceRecord *rrsig,
133 DnsResourceRecord *dnskey) {
134
135 size_t exponent_size, modulus_size;
136 void *exponent, *modulus;
137
138 assert(hash_algorithm);
139 assert(hash);
140 assert(hash_size > 0);
141 assert(rrsig);
142 assert(dnskey);
143
144 if (dnskey->dnskey.key_size < 1)
145 return -EINVAL;
146
147 if (*(uint8_t*) dnskey->dnskey.key == 0) {
148 /* exponent is > 255 bytes long */
149
150 if (dnskey->dnskey.key_size < 3)
151 return -EINVAL;
152
153 exponent_size =
154 ((size_t) (((uint8_t*) dnskey->dnskey.key)[1]) << 8) |
155 ((size_t) ((uint8_t*) dnskey->dnskey.key)[2]);
156
157 if (exponent_size < 256)
158 return -EINVAL;
159
160 if (3 + exponent_size >= dnskey->dnskey.key_size)
161 return -EINVAL;
162
163 exponent = (uint8_t*) dnskey->dnskey.key + 3;
164 modulus = (uint8_t*) dnskey->dnskey.key + 3 + exponent_size;
165 modulus_size = dnskey->dnskey.key_size - 3 - exponent_size;
166
167 } else {
168 /* exponent is <= 255 bytes long */
169 exponent_size = (size_t) ((uint8_t*) dnskey->dnskey.key)[0];
170
171 if (exponent_size <= 0)
172 return -EINVAL;
173
174 if (1 + exponent_size >= dnskey->dnskey.key_size)
175 return -EINVAL;
176
177 exponent = (uint8_t*) dnskey->dnskey.key + 1;
178 modulus = (uint8_t*) dnskey->dnskey.key + 1 + exponent_size;
179 modulus_size = dnskey->dnskey.key_size - 1 - exponent_size;
180 }
181
182 return dnssec_rsa_verify_raw(
183 hash_algorithm,
184 rrsig->rrsig.signature, rrsig->rrsig.signature_size,
185 hash, hash_size,
186 exponent, exponent_size,
187 modulus, modulus_size);
188 }
189
190 static int dnssec_ecdsa_verify_raw(
191 const EVP_MD *hash_algorithm,
192 int curve,
193 const void *signature_r, size_t signature_r_size,
194 const void *signature_s, size_t signature_s_size,
195 const void *data, size_t data_size,
196 const void *key, size_t key_size) {
197 int k;
198
199 DISABLE_WARNING_DEPRECATED_DECLARATIONS;
200 _cleanup_(EC_GROUP_freep) EC_GROUP *ec_group = NULL;
201 _cleanup_(EC_POINT_freep) EC_POINT *p = NULL;
202 _cleanup_(EC_KEY_freep) EC_KEY *eckey = NULL;
203 _cleanup_(BN_CTX_freep) BN_CTX *bctx = NULL;
204 _cleanup_(BN_freep) BIGNUM *r = NULL, *s = NULL;
205 _cleanup_(ECDSA_SIG_freep) ECDSA_SIG *sig = NULL;
206
207 assert(hash_algorithm);
208
209 ec_group = sym_EC_GROUP_new_by_curve_name(curve);
210 if (!ec_group)
211 return -ENOMEM;
212
213 p = sym_EC_POINT_new(ec_group);
214 if (!p)
215 return -ENOMEM;
216
217 bctx = sym_BN_CTX_new();
218 if (!bctx)
219 return -ENOMEM;
220
221 if (sym_EC_POINT_oct2point(ec_group, p, key, key_size, bctx) <= 0)
222 return -EIO;
223
224 eckey = sym_EC_KEY_new();
225 if (!eckey)
226 return -ENOMEM;
227
228 if (sym_EC_KEY_set_group(eckey, ec_group) <= 0)
229 return -EIO;
230
231 if (sym_EC_KEY_set_public_key(eckey, p) <= 0)
232 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
233 "EC_KEY_set_public_key failed: 0x%lx", sym_ERR_get_error());
234
235 if (sym_EC_KEY_check_key(eckey) != 1)
236 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
237 "EC_KEY_check_key failed: 0x%lx", sym_ERR_get_error());
238
239 r = sym_BN_bin2bn(signature_r, signature_r_size, NULL);
240 if (!r)
241 return -EIO;
242
243 s = sym_BN_bin2bn(signature_s, signature_s_size, NULL);
244 if (!s)
245 return -EIO;
246
247 /* TODO: We should eventually use the EVP API once it supports ECDSA signature verification */
248
249 sig = sym_ECDSA_SIG_new();
250 if (!sig)
251 return -ENOMEM;
252
253 if (sym_ECDSA_SIG_set0(sig, r, s) <= 0)
254 return -EIO;
255 r = s = NULL;
256
257 k = sym_ECDSA_do_verify(data, data_size, sig, eckey);
258 if (k < 0)
259 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
260 "Signature verification failed: 0x%lx", sym_ERR_get_error());
261
262 REENABLE_WARNING;
263 return k;
264 }
265
266 static int dnssec_ecdsa_verify(
267 const EVP_MD *hash_algorithm,
268 int algorithm,
269 const void *hash, size_t hash_size,
270 DnsResourceRecord *rrsig,
271 DnsResourceRecord *dnskey) {
272
273 int curve;
274 size_t key_size;
275 uint8_t *q;
276
277 assert(hash);
278 assert(hash_size);
279 assert(rrsig);
280 assert(dnskey);
281
282 if (algorithm == DNSSEC_ALGORITHM_ECDSAP256SHA256) {
283 curve = NID_X9_62_prime256v1; /* NIST P-256 */
284 key_size = 32;
285 } else if (algorithm == DNSSEC_ALGORITHM_ECDSAP384SHA384) {
286 curve = NID_secp384r1; /* NIST P-384 */
287 key_size = 48;
288 } else
289 return -EOPNOTSUPP;
290
291 if (dnskey->dnskey.key_size != key_size * 2)
292 return -EINVAL;
293
294 if (rrsig->rrsig.signature_size != key_size * 2)
295 return -EINVAL;
296
297 q = newa(uint8_t, key_size*2 + 1);
298 q[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
299 memcpy(q+1, dnskey->dnskey.key, key_size*2);
300
301 return dnssec_ecdsa_verify_raw(
302 hash_algorithm,
303 curve,
304 rrsig->rrsig.signature, key_size,
305 (uint8_t*) rrsig->rrsig.signature + key_size, key_size,
306 hash, hash_size,
307 q, key_size*2+1);
308 }
309
310 static int dnssec_eddsa_verify_raw(
311 int curve,
312 const uint8_t *signature, size_t signature_size,
313 const uint8_t *data, size_t data_size,
314 const uint8_t *key, size_t key_size) {
315
316 _cleanup_(EVP_PKEY_freep) EVP_PKEY *evkey = NULL;
317 _cleanup_(EVP_PKEY_CTX_freep) EVP_PKEY_CTX *pctx = NULL;
318 _cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *ctx = NULL;
319 int r;
320
321 assert(curve == NID_ED25519);
322 assert(signature_size == key_size * 2);
323
324 uint8_t *q = newa(uint8_t, signature_size + 1);
325 q[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
326 memcpy(q+1, signature, signature_size);
327
328 evkey = sym_EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519, NULL, key, key_size);
329 if (!evkey)
330 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
331 "EVP_PKEY_new_raw_public_key failed: 0x%lx", sym_ERR_get_error());
332
333 pctx = sym_EVP_PKEY_CTX_new(evkey, NULL);
334 if (!pctx)
335 return -ENOMEM;
336
337 ctx = sym_EVP_MD_CTX_new();
338 if (!ctx)
339 return -ENOMEM;
340
341 /* This prevents EVP_DigestVerifyInit from managing pctx and complicating our free logic. */
342 sym_EVP_MD_CTX_set_pkey_ctx(ctx, pctx);
343
344 /* One might be tempted to use EVP_PKEY_verify_init, but see Ed25519(7ssl). */
345 if (sym_EVP_DigestVerifyInit(ctx, &pctx, NULL, NULL, evkey) <= 0)
346 return -EIO;
347
348 r = sym_EVP_DigestVerify(ctx, signature, signature_size, data, data_size);
349 if (r < 0)
350 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
351 "Signature verification failed: 0x%lx", sym_ERR_get_error());
352
353 return r;
354 }
355
356 static int dnssec_eddsa_verify(
357 int algorithm,
358 const void *data, size_t data_size,
359 DnsResourceRecord *rrsig,
360 DnsResourceRecord *dnskey) {
361 int curve;
362 size_t key_size;
363
364 if (algorithm == DNSSEC_ALGORITHM_ED25519) {
365 curve = NID_ED25519;
366 key_size = 32;
367 } else
368 return -EOPNOTSUPP;
369
370 if (dnskey->dnskey.key_size != key_size)
371 return -EINVAL;
372
373 if (rrsig->rrsig.signature_size != key_size * 2)
374 return -EINVAL;
375
376 return dnssec_eddsa_verify_raw(
377 curve,
378 rrsig->rrsig.signature, rrsig->rrsig.signature_size,
379 data, data_size,
380 dnskey->dnskey.key, key_size);
381 }
382
383 static int md_add_uint8(EVP_MD_CTX *ctx, uint8_t v) {
384 return sym_EVP_DigestUpdate(ctx, &v, sizeof(v));
385 }
386
387 static int md_add_uint16(EVP_MD_CTX *ctx, uint16_t v) {
388 v = htobe16(v);
389 return sym_EVP_DigestUpdate(ctx, &v, sizeof(v));
390 }
391
392 static void fwrite_uint8(FILE *fp, uint8_t v) {
393 fwrite(&v, sizeof(v), 1, fp);
394 }
395
396 static void fwrite_uint16(FILE *fp, uint16_t v) {
397 v = htobe16(v);
398 fwrite(&v, sizeof(v), 1, fp);
399 }
400
401 static void fwrite_uint32(FILE *fp, uint32_t v) {
402 v = htobe32(v);
403 fwrite(&v, sizeof(v), 1, fp);
404 }
405
406 static int dnssec_rrsig_prepare(DnsResourceRecord *rrsig) {
407 int n_key_labels, n_signer_labels;
408 const char *name;
409 int r;
410
411 /* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source
412 * and .n_skip_labels_signer fields so that we can use them later on. */
413
414 assert(rrsig);
415 assert(rrsig->key->type == DNS_TYPE_RRSIG);
416
417 /* Check if this RRSIG RR is already prepared */
418 if (rrsig->n_skip_labels_source != UINT8_MAX)
419 return 0;
420
421 if (rrsig->rrsig.inception > rrsig->rrsig.expiration)
422 return -EINVAL;
423
424 name = dns_resource_key_name(rrsig->key);
425
426 n_key_labels = dns_name_count_labels(name);
427 if (n_key_labels < 0)
428 return n_key_labels;
429 if (rrsig->rrsig.labels > n_key_labels)
430 return -EINVAL;
431
432 n_signer_labels = dns_name_count_labels(rrsig->rrsig.signer);
433 if (n_signer_labels < 0)
434 return n_signer_labels;
435 if (n_signer_labels > rrsig->rrsig.labels)
436 return -EINVAL;
437
438 r = dns_name_skip(name, n_key_labels - n_signer_labels, &name);
439 if (r < 0)
440 return r;
441 if (r == 0)
442 return -EINVAL;
443
444 /* Check if the signer is really a suffix of us */
445 r = dns_name_equal(name, rrsig->rrsig.signer);
446 if (r < 0)
447 return r;
448 if (r == 0)
449 return -EINVAL;
450
451 assert(n_key_labels < UINT8_MAX); /* UINT8_MAX/-1 means unsigned. */
452 rrsig->n_skip_labels_source = n_key_labels - rrsig->rrsig.labels;
453 rrsig->n_skip_labels_signer = n_key_labels - n_signer_labels;
454
455 return 0;
456 }
457
458 static int dnssec_rrsig_expired(DnsResourceRecord *rrsig, usec_t realtime) {
459 usec_t expiration, inception, skew;
460
461 assert(rrsig);
462 assert(rrsig->key->type == DNS_TYPE_RRSIG);
463
464 if (realtime == USEC_INFINITY)
465 realtime = now(CLOCK_REALTIME);
466
467 expiration = rrsig->rrsig.expiration * USEC_PER_SEC;
468 inception = rrsig->rrsig.inception * USEC_PER_SEC;
469
470 /* Consider inverted validity intervals as expired */
471 if (inception > expiration)
472 return true;
473
474 /* Permit a certain amount of clock skew of 10% of the valid
475 * time range. This takes inspiration from unbound's
476 * resolver. */
477 skew = (expiration - inception) / 10;
478 if (skew > SKEW_MAX)
479 skew = SKEW_MAX;
480
481 if (inception < skew)
482 inception = 0;
483 else
484 inception -= skew;
485
486 if (expiration + skew < expiration)
487 expiration = USEC_INFINITY;
488 else
489 expiration += skew;
490
491 return realtime < inception || realtime > expiration;
492 }
493
494 static const EVP_MD* algorithm_to_implementation_id(uint8_t algorithm) {
495
496 /* Translates a DNSSEC signature algorithm into an openssl digest identifier.
497 *
498 * Note that we implement all algorithms listed as "Must implement" and "Recommended to Implement" in
499 * RFC6944. We don't implement any algorithms that are listed as "Optional" or "Must Not Implement".
500 * Specifically, we do not implement RSAMD5, DSASHA1, DH, DSA-NSEC3-SHA1, and GOST-ECC. */
501
502 switch (algorithm) {
503
504 case DNSSEC_ALGORITHM_RSASHA1:
505 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1:
506 return sym_EVP_sha1();
507
508 case DNSSEC_ALGORITHM_RSASHA256:
509 case DNSSEC_ALGORITHM_ECDSAP256SHA256:
510 return sym_EVP_sha256();
511
512 case DNSSEC_ALGORITHM_ECDSAP384SHA384:
513 return sym_EVP_sha384();
514
515 case DNSSEC_ALGORITHM_RSASHA512:
516 return sym_EVP_sha512();
517
518 default:
519 return NULL;
520 }
521 }
522
523 static void dnssec_fix_rrset_ttl(
524 DnsResourceRecord *list[],
525 unsigned n,
526 DnsResourceRecord *rrsig) {
527
528 assert(list);
529 assert(n > 0);
530 assert(rrsig);
531
532 for (unsigned k = 0; k < n; k++) {
533 DnsResourceRecord *rr = list[k];
534
535 /* Pick the TTL as the minimum of the RR's TTL, the
536 * RR's original TTL according to the RRSIG and the
537 * RRSIG's own TTL, see RFC 4035, Section 5.3.3 */
538 rr->ttl = MIN3(rr->ttl, rrsig->rrsig.original_ttl, rrsig->ttl);
539 rr->expiry = rrsig->rrsig.expiration * USEC_PER_SEC;
540
541 /* Copy over information about the signer and wildcard source of synthesis */
542 rr->n_skip_labels_source = rrsig->n_skip_labels_source;
543 rr->n_skip_labels_signer = rrsig->n_skip_labels_signer;
544 }
545
546 rrsig->expiry = rrsig->rrsig.expiration * USEC_PER_SEC;
547 }
548
549 static int dnssec_rrset_serialize_sig(
550 DnsResourceRecord *rrsig,
551 const char *source,
552 DnsResourceRecord **list,
553 size_t list_len,
554 bool wildcard,
555 char **ret_sig_data,
556 size_t *ret_sig_size) {
557
558 _cleanup_(memstream_done) MemStream m = {};
559 uint8_t wire_format_name[DNS_WIRE_FORMAT_HOSTNAME_MAX];
560 DnsResourceRecord *rr;
561 FILE *f;
562 int r;
563
564 assert(rrsig);
565 assert(source);
566 assert(list || list_len == 0);
567 assert(ret_sig_data);
568 assert(ret_sig_size);
569
570 f = memstream_init(&m);
571 if (!f)
572 return -ENOMEM;
573
574 fwrite_uint16(f, rrsig->rrsig.type_covered);
575 fwrite_uint8(f, rrsig->rrsig.algorithm);
576 fwrite_uint8(f, rrsig->rrsig.labels);
577 fwrite_uint32(f, rrsig->rrsig.original_ttl);
578 fwrite_uint32(f, rrsig->rrsig.expiration);
579 fwrite_uint32(f, rrsig->rrsig.inception);
580 fwrite_uint16(f, rrsig->rrsig.key_tag);
581
582 r = dns_name_to_wire_format(rrsig->rrsig.signer, wire_format_name, sizeof(wire_format_name), true);
583 if (r < 0)
584 return r;
585 fwrite(wire_format_name, 1, r, f);
586
587 /* Convert the source of synthesis into wire format */
588 r = dns_name_to_wire_format(source, wire_format_name, sizeof(wire_format_name), true);
589 if (r < 0)
590 return r;
591
592 for (size_t k = 0; k < list_len; k++) {
593 size_t l;
594
595 rr = list[k];
596
597 /* Hash the source of synthesis. If this is a wildcard, then prefix it with the *. label */
598 if (wildcard)
599 fwrite((uint8_t[]) { 1, '*'}, sizeof(uint8_t), 2, f);
600 fwrite(wire_format_name, 1, r, f);
601
602 fwrite_uint16(f, rr->key->type);
603 fwrite_uint16(f, rr->key->class);
604 fwrite_uint32(f, rrsig->rrsig.original_ttl);
605
606 l = DNS_RESOURCE_RECORD_RDATA_SIZE(rr);
607 assert(l <= 0xFFFF);
608
609 fwrite_uint16(f, (uint16_t) l);
610 fwrite(DNS_RESOURCE_RECORD_RDATA(rr), 1, l, f);
611 }
612
613 return memstream_finalize(&m, ret_sig_data, ret_sig_size);
614 }
615
616 static int dnssec_rrset_verify_sig(
617 DnsResourceRecord *rrsig,
618 DnsResourceRecord *dnskey,
619 const char *sig_data,
620 size_t sig_size) {
621
622 assert(rrsig);
623 assert(dnskey);
624 assert(sig_data);
625 assert(sig_size > 0);
626
627 const EVP_MD *md_algorithm;
628
629 uint8_t hash[EVP_MAX_MD_SIZE];
630 unsigned hash_size;
631
632 switch (rrsig->rrsig.algorithm) {
633 case DNSSEC_ALGORITHM_ED25519:
634 return dnssec_eddsa_verify(
635 rrsig->rrsig.algorithm,
636 sig_data, sig_size,
637 rrsig,
638 dnskey);
639 case DNSSEC_ALGORITHM_ED448:
640 return -EOPNOTSUPP;
641 default:
642 /* OK, the RRs are now in canonical order. Let's calculate the digest */
643 md_algorithm = algorithm_to_implementation_id(rrsig->rrsig.algorithm);
644 if (!md_algorithm)
645 return -EOPNOTSUPP;
646
647 _cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *ctx = sym_EVP_MD_CTX_new();
648 if (!ctx)
649 return -ENOMEM;
650
651 /* If the signature algorithm is supported by systemd-resolved but disabled by host policy,
652 * also return -EOPNOTSUPP. */
653 if (sym_EVP_DigestInit_ex(ctx, md_algorithm, NULL) <= 0)
654 return -EOPNOTSUPP;
655
656 if (sym_EVP_DigestUpdate(ctx, sig_data, sig_size) <= 0)
657 return -EIO;
658
659 if (sym_EVP_DigestFinal_ex(ctx, hash, &hash_size) <= 0)
660 return -EIO;
661
662 assert(hash_size > 0);
663 }
664
665 switch (rrsig->rrsig.algorithm) {
666
667 case DNSSEC_ALGORITHM_RSASHA1:
668 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1:
669 case DNSSEC_ALGORITHM_RSASHA256:
670 case DNSSEC_ALGORITHM_RSASHA512:
671 return dnssec_rsa_verify(
672 md_algorithm,
673 hash, hash_size,
674 rrsig,
675 dnskey);
676
677 case DNSSEC_ALGORITHM_ECDSAP256SHA256:
678 case DNSSEC_ALGORITHM_ECDSAP384SHA384:
679 return dnssec_ecdsa_verify(
680 md_algorithm,
681 rrsig->rrsig.algorithm,
682 hash, hash_size,
683 rrsig,
684 dnskey);
685
686 default:
687 assert_not_reached();
688 }
689 }
690
691 int dnssec_verify_rrset(
692 DnsAnswer *a,
693 const DnsResourceKey *key,
694 DnsResourceRecord *rrsig,
695 DnsResourceRecord *dnskey,
696 usec_t realtime,
697 DnssecResult *result) {
698
699 DnsResourceRecord **list, *rr;
700 const char *source, *name;
701 _cleanup_free_ char *sig_data = NULL;
702 size_t sig_size = 0; /* avoid false maybe-uninitialized warning */
703 size_t n = 0;
704 bool wildcard;
705 int r;
706
707 assert(key);
708 assert(rrsig);
709 assert(dnskey);
710 assert(result);
711
712 r = DLOPEN_LIBCRYPTO(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
713 if (r < 0)
714 return r;
715
716 assert(rrsig->key->type == DNS_TYPE_RRSIG);
717 assert(dnskey->key->type == DNS_TYPE_DNSKEY);
718
719 /* Verifies that the RRSet matches the specified "key" in "a",
720 * using the signature "rrsig" and the key "dnskey". It's
721 * assumed that RRSIG and DNSKEY match. */
722
723 r = dnssec_rrsig_prepare(rrsig);
724 if (r == -EINVAL) {
725 *result = DNSSEC_INVALID;
726 return r;
727 }
728 if (r < 0)
729 return r;
730
731 r = dnssec_rrsig_expired(rrsig, realtime);
732 if (r < 0)
733 return r;
734 if (r > 0) {
735 *result = DNSSEC_SIGNATURE_EXPIRED;
736 return 0;
737 }
738
739 name = dns_resource_key_name(key);
740
741 /* Some keys may only appear signed in the zone apex, and are invalid anywhere else. (SOA, NS...) */
742 if (dns_type_apex_only(rrsig->rrsig.type_covered)) {
743 r = dns_name_equal(rrsig->rrsig.signer, name);
744 if (r < 0)
745 return r;
746 if (r == 0) {
747 *result = DNSSEC_INVALID;
748 return 0;
749 }
750 }
751
752 /* OTOH DS RRs may not appear in the zone apex, but are valid everywhere else. */
753 if (rrsig->rrsig.type_covered == DNS_TYPE_DS) {
754 r = dns_name_equal(rrsig->rrsig.signer, name);
755 if (r < 0)
756 return r;
757 if (r > 0) {
758 *result = DNSSEC_INVALID;
759 return 0;
760 }
761 }
762
763 /* Determine the "Source of Synthesis" and whether this is a wildcard RRSIG */
764 r = dns_name_suffix(name, rrsig->rrsig.labels, &source);
765 if (r < 0)
766 return r;
767 if (r > 0 && !dns_type_may_wildcard(rrsig->rrsig.type_covered)) {
768 /* We refuse to validate NSEC3 or SOA RRs that are synthesized from wildcards */
769 *result = DNSSEC_INVALID;
770 return 0;
771 }
772 if (r == 1) {
773 /* If we stripped a single label, then let's see if that maybe was "*". If so, we are not really
774 * synthesized from a wildcard, we are the wildcard itself. Treat that like a normal name. */
775 r = dns_name_startswith(name, "*");
776 if (r < 0)
777 return r;
778 if (r > 0)
779 source = name;
780
781 wildcard = r == 0;
782 } else
783 wildcard = r > 0;
784
785 /* Collect all relevant RRs in a single array, so that we can look at the RRset */
786 list = newa(DnsResourceRecord *, dns_answer_size(a));
787
788 DNS_ANSWER_FOREACH(rr, a) {
789 r = dns_resource_key_equal(key, rr->key);
790 if (r < 0)
791 return r;
792 if (r == 0)
793 continue;
794
795 /* We need the wire format for ordering, and digest calculation */
796 r = dns_resource_record_to_wire_format(rr, true);
797 if (r < 0)
798 return r;
799
800 list[n++] = rr;
801
802 if (n > VERIFY_RRS_MAX)
803 return -E2BIG;
804 }
805
806 if (n <= 0)
807 return -ENODATA;
808
809 /* Bring the RRs into canonical order */
810 typesafe_qsort(list, n, rr_compare);
811
812 r = dnssec_rrset_serialize_sig(rrsig, source, list, n, wildcard,
813 &sig_data, &sig_size);
814 if (r < 0)
815 return r;
816
817 r = dnssec_rrset_verify_sig(rrsig, dnskey, sig_data, sig_size);
818 if (r == -EOPNOTSUPP) {
819 *result = DNSSEC_UNSUPPORTED_ALGORITHM;
820 return 0;
821 }
822 if (r < 0)
823 return r;
824
825 /* Now, fix the ttl, expiry, and remember the synthesizing source and the signer */
826 if (r > 0)
827 dnssec_fix_rrset_ttl(list, n, rrsig);
828
829 if (r == 0)
830 *result = DNSSEC_INVALID;
831 else if (wildcard)
832 *result = DNSSEC_VALIDATED_WILDCARD;
833 else
834 *result = DNSSEC_VALIDATED;
835
836 return 0;
837 }
838
839 int dnssec_rrsig_match_dnskey(DnsResourceRecord *rrsig, DnsResourceRecord *dnskey, bool revoked_ok) {
840
841 assert(rrsig);
842 assert(dnskey);
843
844 /* Checks if the specified DNSKEY RR matches the key used for
845 * the signature in the specified RRSIG RR */
846
847 if (rrsig->key->type != DNS_TYPE_RRSIG)
848 return -EINVAL;
849
850 if (dnskey->key->type != DNS_TYPE_DNSKEY)
851 return 0;
852 if (dnskey->key->class != rrsig->key->class)
853 return 0;
854 if ((dnskey->dnskey.flags & DNSKEY_FLAG_ZONE_KEY) == 0)
855 return 0;
856 if (!revoked_ok && (dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE))
857 return 0;
858 if (dnskey->dnskey.protocol != 3)
859 return 0;
860 if (dnskey->dnskey.algorithm != rrsig->rrsig.algorithm)
861 return 0;
862
863 if (dnssec_keytag(dnskey, false) != rrsig->rrsig.key_tag)
864 return 0;
865
866 return dns_name_equal(dns_resource_key_name(dnskey->key), rrsig->rrsig.signer);
867 }
868
869 int dnssec_key_match_rrsig(const DnsResourceKey *key, DnsResourceRecord *rrsig) {
870 assert(key);
871 assert(rrsig);
872
873 /* Checks if the specified RRSIG RR protects the RRSet of the specified RR key. */
874
875 if (rrsig->key->type != DNS_TYPE_RRSIG)
876 return 0;
877 if (rrsig->key->class != key->class)
878 return 0;
879 if (rrsig->rrsig.type_covered != key->type)
880 return 0;
881
882 return dns_name_equal(dns_resource_key_name(rrsig->key), dns_resource_key_name(key));
883 }
884
885 int dnssec_verify_rrset_search(
886 DnsAnswer *a,
887 const DnsResourceKey *key,
888 DnsAnswer *validated_dnskeys,
889 usec_t realtime,
890 DnssecResult *result,
891 DnsResourceRecord **ret_rrsig) {
892
893 bool found_rrsig = false, found_invalid = false, found_expired_rrsig = false, found_unsupported_algorithm = false;
894 unsigned nvalidations = 0;
895 DnsResourceRecord *rrsig;
896 int r;
897
898 assert(key);
899 assert(result);
900
901 /* Verifies all RRs from "a" that match the key "key" against DNSKEYs in "validated_dnskeys" */
902
903 if (dns_answer_isempty(a))
904 return -ENODATA;
905
906 /* Iterate through each RRSIG RR. */
907 DNS_ANSWER_FOREACH(rrsig, a) {
908 DnsResourceRecord *dnskey;
909 DnsAnswerFlags flags;
910
911 /* Is this an RRSIG RR that applies to RRs matching our key? */
912 r = dnssec_key_match_rrsig(key, rrsig);
913 if (r < 0)
914 return r;
915 if (r == 0)
916 continue;
917
918 found_rrsig = true;
919
920 /* Look for a matching key */
921 DNS_ANSWER_FOREACH_FLAGS(dnskey, flags, validated_dnskeys) {
922 DnssecResult one_result;
923
924 /* Is this a DNSKEY RR that matches they key of our RRSIG? */
925 r = dnssec_rrsig_match_dnskey(rrsig, dnskey, false);
926 if (r < 0)
927 return r;
928 if (r == 0)
929 continue;
930
931 if ((flags & DNS_ANSWER_AUTHENTICATED) == 0) {
932 /* An unauthenticated DNSKEY in validated_dnskeys is a key we are not able to
933 * authenticate, but might still be valid. Record this as an unsupported
934 * algorithm so we can still at least report an insecure answer. */
935 found_unsupported_algorithm = true;
936 continue;
937 }
938
939 /* Take the time here, if it isn't set yet, so
940 * that we do all validations with the same
941 * time. */
942 if (realtime == USEC_INFINITY)
943 realtime = now(CLOCK_REALTIME);
944
945 /* Have we seen an unreasonable number of invalid signatures? */
946 if (nvalidations > DNSSEC_INVALID_MAX) {
947 if (ret_rrsig)
948 *ret_rrsig = NULL;
949 *result = DNSSEC_TOO_MANY_VALIDATIONS;
950 return (int) nvalidations;
951 }
952
953 /* Yay, we found a matching RRSIG with a matching
954 * DNSKEY, awesome. Now let's verify all entries of
955 * the RRSet against the RRSIG and DNSKEY
956 * combination. */
957
958 r = dnssec_verify_rrset(a, key, rrsig, dnskey, realtime, &one_result);
959 if (r < 0)
960 return r;
961
962 nvalidations++;
963
964 switch (one_result) {
965
966 case DNSSEC_VALIDATED:
967 case DNSSEC_VALIDATED_WILDCARD:
968 /* Yay, the RR has been validated,
969 * return immediately, but fix up the expiry */
970 if (ret_rrsig)
971 *ret_rrsig = rrsig;
972
973 *result = one_result;
974 return (int) nvalidations;
975
976 case DNSSEC_INVALID:
977 /* If the signature is invalid, let's try another
978 key and/or signature. After all they
979 key_tags and stuff are not unique, and
980 might be shared by multiple keys. */
981 found_invalid = true;
982 continue;
983
984 case DNSSEC_UNSUPPORTED_ALGORITHM:
985 /* If the key algorithm is
986 unsupported, try another
987 RRSIG/DNSKEY pair, but remember we
988 encountered this, so that we can
989 return a proper error when we
990 encounter nothing better. */
991 found_unsupported_algorithm = true;
992 continue;
993
994 case DNSSEC_SIGNATURE_EXPIRED:
995 /* If the signature is expired, try
996 another one, but remember it, so
997 that we can return this */
998 found_expired_rrsig = true;
999 continue;
1000
1001 default:
1002 assert_not_reached();
1003 }
1004 }
1005 }
1006
1007 if (found_expired_rrsig)
1008 *result = DNSSEC_SIGNATURE_EXPIRED;
1009 else if (found_unsupported_algorithm)
1010 *result = DNSSEC_UNSUPPORTED_ALGORITHM;
1011 else if (found_invalid)
1012 *result = DNSSEC_INVALID;
1013 else if (found_rrsig)
1014 *result = DNSSEC_MISSING_KEY;
1015 else
1016 *result = DNSSEC_NO_SIGNATURE;
1017
1018 if (ret_rrsig)
1019 *ret_rrsig = NULL;
1020
1021 return (int) nvalidations;
1022 }
1023
1024 int dnssec_has_rrsig(DnsAnswer *a, const DnsResourceKey *key) {
1025 DnsResourceRecord *rr;
1026 int r;
1027
1028 /* Checks whether there's at least one RRSIG in 'a' that protects RRs of the specified key */
1029
1030 DNS_ANSWER_FOREACH(rr, a) {
1031 r = dnssec_key_match_rrsig(key, rr);
1032 if (r < 0)
1033 return r;
1034 if (r > 0)
1035 return 1;
1036 }
1037
1038 return 0;
1039 }
1040
1041 static const EVP_MD* digest_to_hash_md(uint8_t algorithm) {
1042
1043 /* Translates a DNSSEC digest algorithm into an openssl digest identifier */
1044
1045 switch (algorithm) {
1046
1047 case DNSSEC_DIGEST_SHA1:
1048 return sym_EVP_sha1();
1049
1050 case DNSSEC_DIGEST_SHA256:
1051 return sym_EVP_sha256();
1052
1053 case DNSSEC_DIGEST_SHA384:
1054 return sym_EVP_sha384();
1055
1056 default:
1057 return NULL;
1058 }
1059 }
1060
1061 int dnssec_verify_dnskey_by_ds(DnsResourceRecord *dnskey, DnsResourceRecord *ds, bool mask_revoke) {
1062 uint8_t wire_format[DNS_WIRE_FORMAT_HOSTNAME_MAX];
1063 size_t encoded_length;
1064 int r;
1065
1066 assert(dnskey);
1067 assert(ds);
1068
1069 r = DLOPEN_LIBCRYPTO(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
1070 if (r < 0)
1071 return r;
1072
1073 /* Implements DNSKEY verification by a DS, according to RFC 4035, section 5.2 */
1074
1075 if (dnskey->key->type != DNS_TYPE_DNSKEY)
1076 return -EINVAL;
1077 if (ds->key->type != DNS_TYPE_DS)
1078 return -EINVAL;
1079 if ((dnskey->dnskey.flags & DNSKEY_FLAG_ZONE_KEY) == 0)
1080 return -EKEYREJECTED;
1081 if (!mask_revoke && (dnskey->dnskey.flags & DNSKEY_FLAG_REVOKE))
1082 return -EKEYREJECTED;
1083 if (dnskey->dnskey.protocol != 3)
1084 return -EKEYREJECTED;
1085
1086 if (dnskey->dnskey.algorithm != ds->ds.algorithm)
1087 return 0;
1088 if (dnssec_keytag(dnskey, mask_revoke) != ds->ds.key_tag)
1089 return 0;
1090
1091 r = dns_name_to_wire_format(dns_resource_key_name(dnskey->key), wire_format, sizeof wire_format, true);
1092 if (r < 0)
1093 return r;
1094 encoded_length = r;
1095
1096 const EVP_MD *md_algorithm = digest_to_hash_md(ds->ds.digest_type);
1097 if (!md_algorithm)
1098 return -EOPNOTSUPP;
1099
1100 _cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *ctx = NULL;
1101 uint8_t result[EVP_MAX_MD_SIZE];
1102
1103 unsigned hash_size = sym_EVP_MD_get_size(md_algorithm);
1104 assert(hash_size > 0);
1105
1106 if (ds->ds.digest_size != hash_size)
1107 return 0;
1108
1109 ctx = sym_EVP_MD_CTX_new();
1110 if (!ctx)
1111 return -ENOMEM;
1112
1113 /* If the digest is supported by systemd-resolved but disabled by host policy, also return -EOPNOTSUPP
1114 */
1115 if (sym_EVP_DigestInit_ex(ctx, md_algorithm, NULL) <= 0)
1116 return -EOPNOTSUPP;
1117
1118 if (sym_EVP_DigestUpdate(ctx, wire_format, encoded_length) <= 0)
1119 return -EIO;
1120
1121 if (mask_revoke)
1122 md_add_uint16(ctx, dnskey->dnskey.flags & ~DNSKEY_FLAG_REVOKE);
1123 else
1124 md_add_uint16(ctx, dnskey->dnskey.flags);
1125
1126 r = md_add_uint8(ctx, dnskey->dnskey.protocol);
1127 if (r <= 0)
1128 return r;
1129 r = md_add_uint8(ctx, dnskey->dnskey.algorithm);
1130 if (r <= 0)
1131 return r;
1132 if (sym_EVP_DigestUpdate(ctx, dnskey->dnskey.key, dnskey->dnskey.key_size) <= 0)
1133 return -EIO;
1134
1135 if (sym_EVP_DigestFinal_ex(ctx, result, NULL) <= 0)
1136 return -EIO;
1137
1138 return memcmp(result, ds->ds.digest, ds->ds.digest_size) == 0;
1139 }
1140
1141 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord *dnskey, DnsAnswer *validated_ds) {
1142 DnsResourceRecord *ds;
1143 DnsAnswerFlags flags;
1144 bool found_unsupported_algorithm = false;
1145 int r;
1146
1147 assert(dnskey);
1148
1149 if (dnskey->key->type != DNS_TYPE_DNSKEY)
1150 return 0;
1151
1152 DNS_ANSWER_FOREACH_FLAGS(ds, flags, validated_ds) {
1153
1154 if ((flags & DNS_ANSWER_AUTHENTICATED) == 0)
1155 continue;
1156
1157 if (ds->key->type != DNS_TYPE_DS)
1158 continue;
1159 if (ds->key->class != dnskey->key->class)
1160 continue;
1161
1162 r = dns_name_equal(dns_resource_key_name(dnskey->key), dns_resource_key_name(ds->key));
1163 if (r < 0)
1164 return r;
1165 if (r == 0)
1166 continue;
1167
1168 r = dnssec_verify_dnskey_by_ds(dnskey, ds, false);
1169 if (r == -EKEYREJECTED)
1170 continue; /* The DNSKEY is revoked or otherwise invalid. */
1171 if (r == -EOPNOTSUPP) {
1172 found_unsupported_algorithm = true;
1173 continue;
1174 }
1175 if (r < 0)
1176 return r;
1177 if (r > 0)
1178 return 1;
1179 }
1180
1181 if (found_unsupported_algorithm)
1182 return -EOPNOTSUPP;
1183
1184 return 0;
1185 }
1186
1187 static const EVP_MD* nsec3_hash_to_hash_md(uint8_t algorithm) {
1188
1189 /* Translates a DNSSEC NSEC3 hash algorithm into an openssl digest identifier */
1190
1191 switch (algorithm) {
1192
1193 case NSEC3_ALGORITHM_SHA1:
1194 return sym_EVP_sha1();
1195
1196 default:
1197 return NULL;
1198 }
1199 }
1200
1201 int dnssec_nsec3_hash(DnsResourceRecord *nsec3, const char *name, void *ret) {
1202 uint8_t wire_format[DNS_WIRE_FORMAT_HOSTNAME_MAX];
1203 int r;
1204
1205 assert(nsec3);
1206 assert(name);
1207 assert(ret);
1208
1209 r = DLOPEN_LIBCRYPTO(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED);
1210 if (r < 0)
1211 return r;
1212
1213 if (nsec3->key->type != DNS_TYPE_NSEC3)
1214 return -EINVAL;
1215
1216 if (nsec3->nsec3.iterations > NSEC3_ITERATIONS_MAX)
1217 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
1218 "Ignoring NSEC3 RR %s with excessive number of iterations.",
1219 dns_resource_record_to_string(nsec3));
1220
1221 const EVP_MD *algorithm = nsec3_hash_to_hash_md(nsec3->nsec3.algorithm);
1222 if (!algorithm)
1223 return -EOPNOTSUPP;
1224
1225 size_t hash_size = sym_EVP_MD_get_size(algorithm);
1226 assert(hash_size > 0);
1227
1228 if (nsec3->nsec3.next_hashed_name_size != hash_size)
1229 return -EINVAL;
1230
1231 _cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *ctx = sym_EVP_MD_CTX_new();
1232 if (!ctx)
1233 return -ENOMEM;
1234
1235 if (sym_EVP_DigestInit_ex(ctx, algorithm, NULL) <= 0)
1236 return -EOPNOTSUPP;
1237
1238 r = dns_name_to_wire_format(name, wire_format, sizeof(wire_format), true);
1239 if (r < 0)
1240 return r;
1241
1242 if (sym_EVP_DigestUpdate(ctx, wire_format, r) <= 0)
1243 return -EIO;
1244 if (sym_EVP_DigestUpdate(ctx, nsec3->nsec3.salt, nsec3->nsec3.salt_size) <= 0)
1245 return -EIO;
1246
1247 uint8_t result[EVP_MAX_MD_SIZE];
1248 if (sym_EVP_DigestFinal_ex(ctx, result, NULL) <= 0)
1249 return -EIO;
1250
1251 for (unsigned k = 0; k < nsec3->nsec3.iterations; k++) {
1252 if (sym_EVP_DigestInit_ex(ctx, algorithm, NULL) <= 0)
1253 return -EOPNOTSUPP;
1254 if (sym_EVP_DigestUpdate(ctx, result, hash_size) <= 0)
1255 return -EIO;
1256 if (sym_EVP_DigestUpdate(ctx, nsec3->nsec3.salt, nsec3->nsec3.salt_size) <= 0)
1257 return -EIO;
1258
1259 if (sym_EVP_DigestFinal_ex(ctx, result, NULL) <= 0)
1260 return -EIO;
1261 }
1262
1263 memcpy(ret, result, hash_size);
1264 return (int) hash_size;
1265 }
1266
1267 static int nsec3_is_good(DnsResourceRecord *rr, DnsResourceRecord *nsec3) {
1268 const char *a, *b;
1269 int r;
1270
1271 assert(rr);
1272
1273 if (rr->key->type != DNS_TYPE_NSEC3)
1274 return 0;
1275
1276 /* RFC 5155, Section 8.2 says we MUST ignore NSEC3 RRs with flags != 0 or 1 */
1277 if (!IN_SET(rr->nsec3.flags, 0, 1))
1278 return 0;
1279
1280 /* Ignore NSEC3 RRs whose algorithm we don't know */
1281 if (!nsec3_hash_to_hash_md(rr->nsec3.algorithm))
1282 return 0;
1283
1284 /* Ignore NSEC3 RRs with an excessive number of required iterations */
1285 if (rr->nsec3.iterations > NSEC3_ITERATIONS_MAX)
1286 return 0;
1287
1288 /* Ignore NSEC3 RRs generated from wildcards. If these NSEC3 RRs weren't correctly signed we can't make this
1289 * check (since rr->n_skip_labels_source is -1), but that's OK, as we won't trust them anyway in that case. */
1290 if (!IN_SET(rr->n_skip_labels_source, 0, UINT8_MAX))
1291 return 0;
1292 /* Ignore NSEC3 RRs that are located anywhere else than one label below the zone */
1293 if (!IN_SET(rr->n_skip_labels_signer, 1, UINT8_MAX))
1294 return 0;
1295
1296 if (!nsec3)
1297 return 1;
1298
1299 /* If a second NSEC3 RR is specified, also check if they are from the same zone. */
1300
1301 if (nsec3 == rr) /* Shortcut */
1302 return 1;
1303
1304 if (rr->key->class != nsec3->key->class)
1305 return 0;
1306 if (rr->nsec3.algorithm != nsec3->nsec3.algorithm)
1307 return 0;
1308 if (rr->nsec3.iterations != nsec3->nsec3.iterations)
1309 return 0;
1310 if (rr->nsec3.salt_size != nsec3->nsec3.salt_size)
1311 return 0;
1312 if (memcmp_safe(rr->nsec3.salt, nsec3->nsec3.salt, rr->nsec3.salt_size) != 0)
1313 return 0;
1314
1315 a = dns_resource_key_name(rr->key);
1316 r = dns_name_parent(&a); /* strip off hash */
1317 if (r <= 0)
1318 return r;
1319
1320 b = dns_resource_key_name(nsec3->key);
1321 r = dns_name_parent(&b); /* strip off hash */
1322 if (r <= 0)
1323 return r;
1324
1325 /* Make sure both have the same parent */
1326 return dns_name_equal(a, b);
1327 }
1328
1329 static int nsec3_hashed_domain_format(const uint8_t *hashed, size_t hashed_size, const char *zone, char **ret) {
1330 _cleanup_free_ char *l = NULL;
1331 char *j;
1332
1333 assert(hashed);
1334 assert(hashed_size > 0);
1335 assert(zone);
1336 assert(ret);
1337
1338 l = base32hexmem(hashed, hashed_size, false);
1339 if (!l)
1340 return -ENOMEM;
1341
1342 j = strjoin(l, ".", zone);
1343 if (!j)
1344 return -ENOMEM;
1345
1346 *ret = j;
1347 return (int) hashed_size;
1348 }
1349
1350 static int nsec3_hashed_domain_make(DnsResourceRecord *nsec3, const char *domain, const char *zone, char **ret) {
1351 uint8_t hashed[DNSSEC_HASH_SIZE_MAX];
1352 int hashed_size;
1353
1354 assert(nsec3);
1355 assert(domain);
1356 assert(zone);
1357 assert(ret);
1358
1359 hashed_size = dnssec_nsec3_hash(nsec3, domain, hashed);
1360 if (hashed_size < 0)
1361 return hashed_size;
1362
1363 return nsec3_hashed_domain_format(hashed, (size_t) hashed_size, zone, ret);
1364 }
1365
1366 /* See RFC 5155, Section 8
1367 * First try to find a NSEC3 record that matches our query precisely, if that fails, find the closest
1368 * enclosure. Secondly, find a proof that there is no closer enclosure and either a proof that there
1369 * is no wildcard domain as a direct descendant of the closest enclosure, or find an NSEC3 record that
1370 * matches the wildcard domain.
1371 *
1372 * Based on this we can prove either the existence of the record in @key, or NXDOMAIN or NODATA, or
1373 * that there is no proof either way. The latter is the case if a proof of non-existence of a given
1374 * name uses an NSEC3 record with the opt-out bit set. Lastly, if we are given insufficient NSEC3 records
1375 * to conclude anything we indicate this by returning NO_RR. */
1376 static int dnssec_test_nsec3(DnsAnswer *answer, DnsResourceKey *key, DnssecNsecResult *result, bool *authenticated, uint32_t *ttl) {
1377 _cleanup_free_ char *next_closer_domain = NULL, *wildcard_domain = NULL;
1378 const char *zone, *p, *pp = NULL, *wildcard;
1379 DnsResourceRecord *rr, *enclosure_rr, *zone_rr, *wildcard_rr = NULL;
1380 DnsAnswerFlags flags;
1381 int hashed_size, r;
1382 bool a, no_closer = false, no_wildcard = false, optout = false;
1383
1384 assert(key);
1385 assert(result);
1386
1387 /* First step, find the zone name and the NSEC3 parameters of the zone.
1388 * it is sufficient to look for the longest common suffix we find with
1389 * any NSEC3 RR in the response. Any NSEC3 record will do as all NSEC3
1390 * records from a given zone in a response must use the same
1391 * parameters. */
1392 zone = dns_resource_key_name(key);
1393 for (;;) {
1394 DNS_ANSWER_FOREACH_FLAGS(zone_rr, flags, answer) {
1395 r = nsec3_is_good(zone_rr, NULL);
1396 if (r < 0)
1397 return r;
1398 if (r == 0)
1399 continue;
1400
1401 r = dns_name_equal_skip(dns_resource_key_name(zone_rr->key), 1, zone);
1402 if (r < 0)
1403 return r;
1404 if (r > 0)
1405 goto found_zone;
1406 }
1407
1408 /* Strip one label from the front */
1409 r = dns_name_parent(&zone);
1410 if (r < 0)
1411 return r;
1412 if (r == 0)
1413 break;
1414 }
1415
1416 *result = DNSSEC_NSEC_NO_RR;
1417 return 0;
1418
1419 found_zone:
1420 /* Second step, find the closest encloser NSEC3 RR in 'answer' that matches 'key' */
1421 p = dns_resource_key_name(key);
1422 for (;;) {
1423 _cleanup_free_ char *hashed_domain = NULL;
1424
1425 hashed_size = nsec3_hashed_domain_make(zone_rr, p, zone, &hashed_domain);
1426 if (hashed_size == -EOPNOTSUPP) {
1427 *result = DNSSEC_NSEC_UNSUPPORTED_ALGORITHM;
1428 return 0;
1429 }
1430 if (hashed_size < 0)
1431 return hashed_size;
1432
1433 DNS_ANSWER_FOREACH_FLAGS(enclosure_rr, flags, answer) {
1434
1435 r = nsec3_is_good(enclosure_rr, zone_rr);
1436 if (r < 0)
1437 return r;
1438 if (r == 0)
1439 continue;
1440
1441 if (enclosure_rr->nsec3.next_hashed_name_size != (size_t) hashed_size)
1442 continue;
1443
1444 r = dns_name_equal(dns_resource_key_name(enclosure_rr->key), hashed_domain);
1445 if (r < 0)
1446 return r;
1447 if (r > 0) {
1448 a = flags & DNS_ANSWER_AUTHENTICATED;
1449 goto found_closest_encloser;
1450 }
1451 }
1452
1453 /* We didn't find the closest encloser with this name,
1454 * but let's remember this domain name, it might be
1455 * the next closer name */
1456
1457 pp = p;
1458
1459 /* Strip one label from the front */
1460 r = dns_name_parent(&p);
1461 if (r < 0)
1462 return r;
1463 if (r == 0)
1464 break;
1465 }
1466
1467 *result = DNSSEC_NSEC_NO_RR;
1468 return 0;
1469
1470 found_closest_encloser:
1471 /* We found a closest encloser in 'p'; next closer is 'pp' */
1472
1473 if (!pp) {
1474 /* We have an exact match! If we area looking for a DS RR, then we must insist that we got the NSEC3 RR
1475 * from the parent. Otherwise the one from the child. Do so, by checking whether SOA and NS are
1476 * appropriately set. */
1477
1478 if (key->type == DNS_TYPE_DS) {
1479 if (bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_SOA))
1480 return -EBADMSG;
1481 } else {
1482 if (bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_NS) &&
1483 !bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_SOA))
1484 return -EBADMSG;
1485 }
1486
1487 /* No next closer NSEC3 RR. That means there's a direct NSEC3 RR for our key. */
1488 if (bitmap_isset(enclosure_rr->nsec3.types, key->type))
1489 *result = DNSSEC_NSEC_FOUND;
1490 else if (bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_CNAME))
1491 *result = DNSSEC_NSEC_CNAME;
1492 else
1493 *result = DNSSEC_NSEC_NODATA;
1494
1495 if (authenticated)
1496 *authenticated = a;
1497 if (ttl)
1498 *ttl = enclosure_rr->ttl;
1499
1500 return 0;
1501 }
1502
1503 /* Ensure this is not a DNAME domain, see RFC5155, section 8.3. */
1504 if (bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_DNAME))
1505 return -EBADMSG;
1506
1507 /* Ensure that this data is from the delegated domain
1508 * (i.e. originates from the "lower" DNS server), and isn't
1509 * just glue records (i.e. doesn't originate from the "upper"
1510 * DNS server). */
1511 if (bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_NS) &&
1512 !bitmap_isset(enclosure_rr->nsec3.types, DNS_TYPE_SOA))
1513 return -EBADMSG;
1514
1515 /* Prove that there is no next closer and whether or not there is a wildcard domain. */
1516
1517 wildcard = strjoina("*.", p);
1518 r = nsec3_hashed_domain_make(enclosure_rr, wildcard, zone, &wildcard_domain);
1519 if (r < 0)
1520 return r;
1521 if (r != hashed_size)
1522 return -EBADMSG;
1523
1524 r = nsec3_hashed_domain_make(enclosure_rr, pp, zone, &next_closer_domain);
1525 if (r < 0)
1526 return r;
1527 if (r != hashed_size)
1528 return -EBADMSG;
1529
1530 DNS_ANSWER_FOREACH_FLAGS(rr, flags, answer) {
1531 _cleanup_free_ char *next_hashed_domain = NULL;
1532
1533 r = nsec3_is_good(rr, zone_rr);
1534 if (r < 0)
1535 return r;
1536 if (r == 0)
1537 continue;
1538
1539 r = nsec3_hashed_domain_format(rr->nsec3.next_hashed_name, rr->nsec3.next_hashed_name_size, zone, &next_hashed_domain);
1540 if (r < 0)
1541 return r;
1542
1543 r = dns_name_between(dns_resource_key_name(rr->key), next_closer_domain, next_hashed_domain);
1544 if (r < 0)
1545 return r;
1546 if (r > 0) {
1547 if (rr->nsec3.flags & 1)
1548 optout = true;
1549
1550 a = a && (flags & DNS_ANSWER_AUTHENTICATED);
1551
1552 no_closer = true;
1553 }
1554
1555 r = dns_name_equal(dns_resource_key_name(rr->key), wildcard_domain);
1556 if (r < 0)
1557 return r;
1558 if (r > 0) {
1559 a = a && (flags & DNS_ANSWER_AUTHENTICATED);
1560
1561 wildcard_rr = rr;
1562 }
1563
1564 r = dns_name_between(dns_resource_key_name(rr->key), wildcard_domain, next_hashed_domain);
1565 if (r < 0)
1566 return r;
1567 if (r > 0) {
1568 if (rr->nsec3.flags & 1)
1569 /* This only makes sense if we have a wildcard delegation, which is
1570 * very unlikely, see RFC 4592, Section 4.2, but we cannot rely on
1571 * this not happening, so hence cannot simply conclude NXDOMAIN as
1572 * we would wish */
1573 optout = true;
1574
1575 a = a && (flags & DNS_ANSWER_AUTHENTICATED);
1576
1577 no_wildcard = true;
1578 }
1579 }
1580
1581 if (wildcard_rr && no_wildcard)
1582 return -EBADMSG;
1583
1584 if (!no_closer) {
1585 *result = DNSSEC_NSEC_NO_RR;
1586 return 0;
1587 }
1588
1589 if (wildcard_rr) {
1590 /* A wildcard exists that matches our query. */
1591 if (optout)
1592 /* This is not specified in any RFC to the best of my knowledge, but
1593 * if the next closer enclosure is covered by an opt-out NSEC3 RR
1594 * it means that we cannot prove that the source of synthesis is
1595 * correct, as there may be a closer match. */
1596 *result = DNSSEC_NSEC_OPTOUT;
1597 else if (bitmap_isset(wildcard_rr->nsec3.types, key->type))
1598 *result = DNSSEC_NSEC_FOUND;
1599 else if (bitmap_isset(wildcard_rr->nsec3.types, DNS_TYPE_CNAME))
1600 *result = DNSSEC_NSEC_CNAME;
1601 else
1602 *result = DNSSEC_NSEC_NODATA;
1603 } else {
1604 if (optout)
1605 /* The RFC only specifies that we have to care for optout for NODATA for
1606 * DS records. However, children of an insecure opt-out delegation should
1607 * also be considered opt-out, rather than verified NXDOMAIN.
1608 * Note that we do not require a proof of wildcard non-existence if the
1609 * next closer domain is covered by an opt-out, as that would not provide
1610 * any additional information. */
1611 *result = DNSSEC_NSEC_OPTOUT;
1612 else if (no_wildcard)
1613 *result = DNSSEC_NSEC_NXDOMAIN;
1614 else {
1615 *result = DNSSEC_NSEC_NO_RR;
1616
1617 return 0;
1618 }
1619 }
1620
1621 if (authenticated)
1622 *authenticated = a;
1623
1624 if (ttl)
1625 *ttl = enclosure_rr->ttl;
1626
1627 return 0;
1628 }
1629
1630 static int dnssec_nsec_wildcard_equal(DnsResourceRecord *rr, const char *name) {
1631 char label[DNS_LABEL_MAX+1];
1632 const char *n;
1633 int r;
1634
1635 assert(rr);
1636 assert(rr->key->type == DNS_TYPE_NSEC);
1637
1638 /* Checks whether the specified RR has a name beginning in "*.", and if the rest is a suffix of our name */
1639
1640 if (rr->n_skip_labels_source != 1)
1641 return 0;
1642
1643 n = dns_resource_key_name(rr->key);
1644 r = dns_label_unescape(&n, label, sizeof label, 0);
1645 if (r <= 0)
1646 return r;
1647 if (r != 1 || label[0] != '*')
1648 return 0;
1649
1650 return dns_name_endswith(name, n);
1651 }
1652
1653 static int dnssec_nsec_in_path(DnsResourceRecord *rr, const char *name) {
1654 const char *nn, *common_suffix;
1655 int r;
1656
1657 assert(rr);
1658 assert(rr->key->type == DNS_TYPE_NSEC);
1659
1660 /* Checks whether the specified nsec RR indicates that name is an empty non-terminal (ENT)
1661 *
1662 * A couple of examples:
1663 *
1664 * NSEC bar → waldo.foo.bar: indicates that foo.bar exists and is an ENT
1665 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that xoo.bar and zzz.xoo.bar exist and are ENTs
1666 * NSEC yyy.zzz.xoo.bar → bar: indicates pretty much nothing about ENTs
1667 */
1668
1669 /* First, determine parent of next domain. */
1670 nn = rr->nsec.next_domain_name;
1671 r = dns_name_parent(&nn);
1672 if (r <= 0)
1673 return r;
1674
1675 /* If the name we just determined is not equal or child of the name we are interested in, then we can't say
1676 * anything at all. */
1677 r = dns_name_endswith(nn, name);
1678 if (r <= 0)
1679 return r;
1680
1681 /* If the name we are interested in is not a prefix of the common suffix of the NSEC RR's owner and next domain names, then we can't say anything either. */
1682 r = dns_name_common_suffix(dns_resource_key_name(rr->key), rr->nsec.next_domain_name, &common_suffix);
1683 if (r < 0)
1684 return r;
1685
1686 return dns_name_endswith(name, common_suffix);
1687 }
1688
1689 static int dnssec_nsec_from_parent_zone(DnsResourceRecord *rr, const char *name) {
1690 int r;
1691
1692 assert(rr);
1693 assert(rr->key->type == DNS_TYPE_NSEC);
1694
1695 /* Checks whether this NSEC originates to the parent zone or the child zone. */
1696
1697 r = dns_name_parent(&name);
1698 if (r <= 0)
1699 return r;
1700
1701 r = dns_name_equal(name, dns_resource_key_name(rr->key));
1702 if (r <= 0)
1703 return r;
1704
1705 /* DNAME, and NS without SOA is an indication for a delegation. */
1706 if (bitmap_isset(rr->nsec.types, DNS_TYPE_DNAME))
1707 return 1;
1708
1709 if (bitmap_isset(rr->nsec.types, DNS_TYPE_NS) && !bitmap_isset(rr->nsec.types, DNS_TYPE_SOA))
1710 return 1;
1711
1712 return 0;
1713 }
1714
1715 static int dnssec_nsec_covers(DnsResourceRecord *rr, const char *name) {
1716 const char *signer;
1717 int r;
1718
1719 assert(rr);
1720 assert(rr->key->type == DNS_TYPE_NSEC);
1721
1722 /* Checks whether the name is covered by this NSEC RR. This means, that the name is somewhere below the NSEC's
1723 * signer name, and between the NSEC's two names. */
1724
1725 r = dns_resource_record_signer(rr, &signer);
1726 if (r < 0)
1727 return r;
1728
1729 r = dns_name_endswith(name, signer); /* this NSEC isn't suitable the name is not in the signer's domain */
1730 if (r <= 0)
1731 return r;
1732
1733 return dns_name_between(dns_resource_key_name(rr->key), name, rr->nsec.next_domain_name);
1734 }
1735
1736 static int dnssec_nsec_generate_wildcard(DnsResourceRecord *rr, const char *name, char **wc) {
1737 const char *common_suffix1, *common_suffix2, *signer;
1738 int r, labels1, labels2;
1739
1740 assert(rr);
1741 assert(rr->key->type == DNS_TYPE_NSEC);
1742
1743 /* Generates "Wildcard at the Closest Encloser" for the given name and NSEC RR. */
1744
1745 r = dns_resource_record_signer(rr, &signer);
1746 if (r < 0)
1747 return r;
1748
1749 r = dns_name_endswith(name, signer); /* this NSEC isn't suitable the name is not in the signer's domain */
1750 if (r <= 0)
1751 return r;
1752
1753 r = dns_name_common_suffix(name, dns_resource_key_name(rr->key), &common_suffix1);
1754 if (r < 0)
1755 return r;
1756
1757 r = dns_name_common_suffix(name, rr->nsec.next_domain_name, &common_suffix2);
1758 if (r < 0)
1759 return r;
1760
1761 labels1 = dns_name_count_labels(common_suffix1);
1762 if (labels1 < 0)
1763 return labels1;
1764
1765 labels2 = dns_name_count_labels(common_suffix2);
1766 if (labels2 < 0)
1767 return labels2;
1768
1769 if (labels1 > labels2)
1770 r = dns_name_concat("*", common_suffix1, 0, wc);
1771 else
1772 r = dns_name_concat("*", common_suffix2, 0, wc);
1773
1774 if (r < 0)
1775 return r;
1776
1777 return 0;
1778 }
1779
1780 int dnssec_nsec_test(DnsAnswer *answer, DnsResourceKey *key, DnssecNsecResult *result, bool *authenticated, uint32_t *ttl) {
1781 bool have_nsec3 = false, covering_rr_authenticated = false, wildcard_rr_authenticated = false;
1782 DnsResourceRecord *rr, *covering_rr = NULL, *wildcard_rr = NULL;
1783 DnsAnswerFlags flags;
1784 const char *name;
1785 int r;
1786
1787 assert(key);
1788 assert(result);
1789
1790 /* Look for any NSEC/NSEC3 RRs that say something about the specified key. */
1791
1792 name = dns_resource_key_name(key);
1793
1794 DNS_ANSWER_FOREACH_FLAGS(rr, flags, answer) {
1795
1796 if (rr->key->class != key->class)
1797 continue;
1798
1799 have_nsec3 = have_nsec3 || (rr->key->type == DNS_TYPE_NSEC3);
1800
1801 if (rr->key->type != DNS_TYPE_NSEC)
1802 continue;
1803
1804 /* The following checks only make sense for NSEC RRs that are not expanded from a wildcard */
1805 r = dns_resource_record_is_synthetic(rr);
1806 if (r == -ENODATA) /* No signing RR known. */
1807 continue;
1808 if (r < 0)
1809 return r;
1810 if (r > 0)
1811 continue;
1812
1813 /* Check if this is a direct match. If so, we have encountered a NODATA case */
1814 r = dns_name_equal(dns_resource_key_name(rr->key), name);
1815 if (r < 0)
1816 return r;
1817 if (r == 0) {
1818 /* If it's not a direct match, maybe it's a wild card match? */
1819 r = dnssec_nsec_wildcard_equal(rr, name);
1820 if (r < 0)
1821 return r;
1822 }
1823 if (r > 0) {
1824 if (key->type == DNS_TYPE_DS) {
1825 /* If we look for a DS RR and the server sent us the NSEC RR of the child zone
1826 * we have a problem. For DS RRs we want the NSEC RR from the parent */
1827 if (bitmap_isset(rr->nsec.types, DNS_TYPE_SOA))
1828 continue;
1829 } else {
1830 /* For all RR types, ensure that if NS is set SOA is set too, so that we know
1831 * we got the child's NSEC. */
1832 if (bitmap_isset(rr->nsec.types, DNS_TYPE_NS) &&
1833 !bitmap_isset(rr->nsec.types, DNS_TYPE_SOA))
1834 continue;
1835 }
1836
1837 if (bitmap_isset(rr->nsec.types, key->type))
1838 *result = DNSSEC_NSEC_FOUND;
1839 else if (bitmap_isset(rr->nsec.types, DNS_TYPE_CNAME))
1840 *result = DNSSEC_NSEC_CNAME;
1841 else
1842 *result = DNSSEC_NSEC_NODATA;
1843
1844 if (authenticated)
1845 *authenticated = flags & DNS_ANSWER_AUTHENTICATED;
1846 if (ttl)
1847 *ttl = rr->ttl;
1848
1849 return 0;
1850 }
1851
1852 /* Check if the name we are looking for is an empty non-terminal within the owner or next name
1853 * of the NSEC RR. */
1854 r = dnssec_nsec_in_path(rr, name);
1855 if (r < 0)
1856 return r;
1857 if (r > 0) {
1858 *result = DNSSEC_NSEC_NODATA;
1859
1860 if (authenticated)
1861 *authenticated = flags & DNS_ANSWER_AUTHENTICATED;
1862 if (ttl)
1863 *ttl = rr->ttl;
1864
1865 return 0;
1866 }
1867
1868 /* The following two "covering" checks, are not useful if the NSEC is from the parent */
1869 r = dnssec_nsec_from_parent_zone(rr, name);
1870 if (r < 0)
1871 return r;
1872 if (r > 0)
1873 continue;
1874
1875 /* Check if this NSEC RR proves the absence of an explicit RR under this name */
1876 r = dnssec_nsec_covers(rr, name);
1877 if (r < 0)
1878 return r;
1879 if (r > 0 && (!covering_rr || !covering_rr_authenticated)) {
1880 covering_rr = rr;
1881 covering_rr_authenticated = flags & DNS_ANSWER_AUTHENTICATED;
1882 }
1883 }
1884
1885 if (covering_rr) {
1886 _cleanup_free_ char *wc = NULL;
1887 r = dnssec_nsec_generate_wildcard(covering_rr, name, &wc);
1888 if (r < 0)
1889 return r;
1890
1891 DNS_ANSWER_FOREACH_FLAGS(rr, flags, answer) {
1892
1893 if (rr->key->class != key->class)
1894 continue;
1895
1896 if (rr->key->type != DNS_TYPE_NSEC)
1897 continue;
1898
1899 /* Check if this NSEC RR proves the nonexistence of the wildcard */
1900 r = dnssec_nsec_covers(rr, wc);
1901 if (r < 0)
1902 return r;
1903 if (r > 0 && (!wildcard_rr || !wildcard_rr_authenticated)) {
1904 wildcard_rr = rr;
1905 wildcard_rr_authenticated = flags & DNS_ANSWER_AUTHENTICATED;
1906 }
1907 }
1908 }
1909
1910 if (covering_rr && wildcard_rr) {
1911 /* If we could prove that neither the name itself, nor the wildcard at the closest encloser exists, we
1912 * proved the NXDOMAIN case. */
1913 *result = DNSSEC_NSEC_NXDOMAIN;
1914
1915 if (authenticated)
1916 *authenticated = covering_rr_authenticated && wildcard_rr_authenticated;
1917 if (ttl)
1918 *ttl = MIN(covering_rr->ttl, wildcard_rr->ttl);
1919
1920 return 0;
1921 }
1922
1923 /* OK, this was not sufficient. Let's see if NSEC3 can help. */
1924 if (have_nsec3)
1925 return dnssec_test_nsec3(answer, key, result, authenticated, ttl);
1926
1927 /* No appropriate NSEC RR found, report this. */
1928 *result = DNSSEC_NSEC_NO_RR;
1929 return 0;
1930 }
1931
1932 static int dnssec_nsec_test_enclosed(DnsAnswer *answer, uint16_t type, const char *name, const char *zone, bool *authenticated) {
1933 DnsResourceRecord *rr;
1934 DnsAnswerFlags flags;
1935 int r;
1936
1937 assert(name);
1938 assert(zone);
1939
1940 /* Checks whether there's an NSEC/NSEC3 that proves that the specified 'name' is non-existing in the specified
1941 * 'zone'. The 'zone' must be a suffix of the 'name'. */
1942
1943 DNS_ANSWER_FOREACH_FLAGS(rr, flags, answer) {
1944 bool found = false;
1945
1946 if (rr->key->type != type && type != DNS_TYPE_ANY)
1947 continue;
1948
1949 switch (rr->key->type) {
1950
1951 case DNS_TYPE_NSEC:
1952
1953 /* We only care for NSEC RRs from the indicated zone */
1954 r = dns_resource_record_is_signer(rr, zone);
1955 if (r < 0)
1956 return r;
1957 if (r == 0)
1958 continue;
1959
1960 r = dns_name_between(dns_resource_key_name(rr->key), name, rr->nsec.next_domain_name);
1961 if (r < 0)
1962 return r;
1963
1964 found = r > 0;
1965 break;
1966
1967 case DNS_TYPE_NSEC3: {
1968 _cleanup_free_ char *hashed_domain = NULL, *next_hashed_domain = NULL;
1969
1970 /* We only care for NSEC3 RRs from the indicated zone */
1971 r = dns_resource_record_is_signer(rr, zone);
1972 if (r < 0)
1973 return r;
1974 if (r == 0)
1975 continue;
1976
1977 r = nsec3_is_good(rr, NULL);
1978 if (r < 0)
1979 return r;
1980 if (r == 0)
1981 break;
1982
1983 /* Format the domain we are testing with the NSEC3 RR's hash function */
1984 r = nsec3_hashed_domain_make(
1985 rr,
1986 name,
1987 zone,
1988 &hashed_domain);
1989 if (r < 0)
1990 return r;
1991 if ((size_t) r != rr->nsec3.next_hashed_name_size)
1992 break;
1993
1994 /* Format the NSEC3's next hashed name as proper domain name */
1995 r = nsec3_hashed_domain_format(
1996 rr->nsec3.next_hashed_name,
1997 rr->nsec3.next_hashed_name_size,
1998 zone,
1999 &next_hashed_domain);
2000 if (r < 0)
2001 return r;
2002
2003 r = dns_name_between(dns_resource_key_name(rr->key), hashed_domain, next_hashed_domain);
2004 if (r < 0)
2005 return r;
2006
2007 found = r > 0;
2008 break;
2009 }
2010
2011 default:
2012 continue;
2013 }
2014
2015 if (found) {
2016 if (authenticated)
2017 *authenticated = flags & DNS_ANSWER_AUTHENTICATED;
2018 return 1;
2019 }
2020 }
2021
2022 return 0;
2023 }
2024
2025 static int dnssec_test_positive_wildcard_nsec3(
2026 DnsAnswer *answer,
2027 const char *name,
2028 const char *source,
2029 const char *zone,
2030 bool *authenticated) {
2031
2032 const char *next_closer = NULL;
2033 int r;
2034
2035 /* Run a positive NSEC3 wildcard proof. Specifically:
2036 *
2037 * A proof that the "next closer" of the generating wildcard does not exist.
2038 *
2039 * Note a key difference between the NSEC3 and NSEC versions of the proof. NSEC RRs don't have to exist for
2040 * empty non-transients. NSEC3 RRs however have to. This means it's sufficient to check if the next closer name
2041 * exists for the NSEC3 RR and we are done.
2042 *
2043 * To prove that a.b.c.d.e.f is rightfully synthesized from a wildcard *.d.e.f all we have to check is that
2044 * c.d.e.f does not exist. */
2045
2046 for (;;) {
2047 next_closer = name;
2048 r = dns_name_parent(&name);
2049 if (r <= 0)
2050 return r;
2051
2052 r = dns_name_equal(name, source);
2053 if (r < 0)
2054 return r;
2055 if (r > 0)
2056 break;
2057 }
2058
2059 return dnssec_nsec_test_enclosed(answer, DNS_TYPE_NSEC3, next_closer, zone, authenticated);
2060 }
2061
2062 static int dnssec_test_positive_wildcard_nsec(
2063 DnsAnswer *answer,
2064 const char *name,
2065 const char *source,
2066 const char *zone,
2067 bool *_authenticated) {
2068
2069 bool authenticated = true;
2070 int r;
2071
2072 assert(_authenticated);
2073
2074 /* Run a positive NSEC wildcard proof. Specifically:
2075 *
2076 * A proof that there's neither a wildcard name nor a non-wildcard name that is a suffix of the name "name" and
2077 * a prefix of the synthesizing source "source" in the zone "zone".
2078 *
2079 * See RFC 5155, Section 8.8 and RFC 4035, Section 5.3.4
2080 *
2081 * Note that if we want to prove that a.b.c.d.e.f is rightfully synthesized from a wildcard *.d.e.f, then we
2082 * have to prove that none of the following exist:
2083 *
2084 * 1) a.b.c.d.e.f
2085 * 2) *.b.c.d.e.f
2086 * 3) b.c.d.e.f
2087 * 4) *.c.d.e.f
2088 * 5) c.d.e.f
2089 */
2090
2091 for (;;) {
2092 _cleanup_free_ char *wc = NULL;
2093 bool a = false;
2094
2095 /* Check if there's an NSEC or NSEC3 RR that proves that the mame we determined is really non-existing,
2096 * i.e between the owner name and the next name of an NSEC RR. */
2097 r = dnssec_nsec_test_enclosed(answer, DNS_TYPE_NSEC, name, zone, &a);
2098 if (r <= 0)
2099 return r;
2100
2101 authenticated = authenticated && a;
2102
2103 /* Strip one label off */
2104 r = dns_name_parent(&name);
2105 if (r <= 0)
2106 return r;
2107
2108 /* Did we reach the source of synthesis? */
2109 r = dns_name_equal(name, source);
2110 if (r < 0)
2111 return r;
2112 if (r > 0) {
2113 /* Successful exit */
2114 *_authenticated = authenticated;
2115 return 1;
2116 }
2117
2118 /* Safety check, that the source of synthesis is still our suffix */
2119 r = dns_name_endswith(name, source);
2120 if (r < 0)
2121 return r;
2122 if (r == 0)
2123 return -EBADMSG;
2124
2125 /* Replace the label we stripped off with an asterisk */
2126 wc = strjoin("*.", name);
2127 if (!wc)
2128 return -ENOMEM;
2129
2130 /* And check if the proof holds for the asterisk name, too */
2131 r = dnssec_nsec_test_enclosed(answer, DNS_TYPE_NSEC, wc, zone, &a);
2132 if (r <= 0)
2133 return r;
2134
2135 authenticated = authenticated && a;
2136 /* In the next iteration we'll check the non-asterisk-prefixed version */
2137 }
2138 }
2139
2140 int dnssec_test_positive_wildcard(
2141 DnsAnswer *answer,
2142 const char *name,
2143 const char *source,
2144 const char *zone,
2145 bool *authenticated) {
2146
2147 int r;
2148
2149 assert(name);
2150 assert(source);
2151 assert(zone);
2152 assert(authenticated);
2153
2154 r = dns_answer_contains_zone_nsec3(answer, zone);
2155 if (r < 0)
2156 return r;
2157 if (r > 0)
2158 return dnssec_test_positive_wildcard_nsec3(answer, name, source, zone, authenticated);
2159 else
2160 return dnssec_test_positive_wildcard_nsec(answer, name, source, zone, authenticated);
2161 }
2162
2163 #else
2164
2165 int dnssec_verify_rrset(
2166 DnsAnswer *a,
2167 const DnsResourceKey *key,
2168 DnsResourceRecord *rrsig,
2169 DnsResourceRecord *dnskey,
2170 usec_t realtime,
2171 DnssecResult *result) {
2172
2173 return -EOPNOTSUPP;
2174 }
2175
2176 int dnssec_rrsig_match_dnskey(DnsResourceRecord *rrsig, DnsResourceRecord *dnskey, bool revoked_ok) {
2177
2178 return -EOPNOTSUPP;
2179 }
2180
2181 int dnssec_key_match_rrsig(const DnsResourceKey *key, DnsResourceRecord *rrsig) {
2182
2183 return -EOPNOTSUPP;
2184 }
2185
2186 int dnssec_verify_rrset_search(
2187 DnsAnswer *a,
2188 const DnsResourceKey *key,
2189 DnsAnswer *validated_dnskeys,
2190 usec_t realtime,
2191 DnssecResult *result,
2192 DnsResourceRecord **ret_rrsig) {
2193
2194 return -EOPNOTSUPP;
2195 }
2196
2197 int dnssec_has_rrsig(DnsAnswer *a, const DnsResourceKey *key) {
2198
2199 return -EOPNOTSUPP;
2200 }
2201
2202 int dnssec_verify_dnskey_by_ds(DnsResourceRecord *dnskey, DnsResourceRecord *ds, bool mask_revoke) {
2203
2204 return -EOPNOTSUPP;
2205 }
2206
2207 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord *dnskey, DnsAnswer *validated_ds) {
2208
2209 return -EOPNOTSUPP;
2210 }
2211
2212 int dnssec_nsec3_hash(DnsResourceRecord *nsec3, const char *name, void *ret) {
2213
2214 return -EOPNOTSUPP;
2215 }
2216
2217 int dnssec_nsec_test(DnsAnswer *answer, DnsResourceKey *key, DnssecNsecResult *result, bool *authenticated, uint32_t *ttl) {
2218
2219 return -EOPNOTSUPP;
2220 }
2221
2222 int dnssec_test_positive_wildcard(
2223 DnsAnswer *answer,
2224 const char *name,
2225 const char *source,
2226 const char *zone,
2227 bool *authenticated) {
2228
2229 return -EOPNOTSUPP;
2230 }
2231
2232 #endif
2233
2234 static const char* const dnssec_result_table[_DNSSEC_RESULT_MAX] = {
2235 [DNSSEC_VALIDATED] = "validated",
2236 [DNSSEC_VALIDATED_WILDCARD] = "validated-wildcard",
2237 [DNSSEC_INVALID] = "invalid",
2238 [DNSSEC_SIGNATURE_EXPIRED] = "signature-expired",
2239 [DNSSEC_UNSUPPORTED_ALGORITHM] = "unsupported-algorithm",
2240 [DNSSEC_NO_SIGNATURE] = "no-signature",
2241 [DNSSEC_MISSING_KEY] = "missing-key",
2242 [DNSSEC_UNSIGNED] = "unsigned",
2243 [DNSSEC_FAILED_AUXILIARY] = "failed-auxiliary",
2244 [DNSSEC_NSEC_MISMATCH] = "nsec-mismatch",
2245 [DNSSEC_INCOMPATIBLE_SERVER] = "incompatible-server",
2246 [DNSSEC_UPSTREAM_FAILURE] = "upstream-failure",
2247 [DNSSEC_TOO_MANY_VALIDATIONS] = "too-many-validations",
2248 };
2249 DEFINE_STRING_TABLE_LOOKUP(dnssec_result, DnssecResult);
2250
2251 static const char* const dnssec_verdict_table[_DNSSEC_VERDICT_MAX] = {
2252 [DNSSEC_SECURE] = "secure",
2253 [DNSSEC_INSECURE] = "insecure",
2254 [DNSSEC_BOGUS] = "bogus",
2255 [DNSSEC_INDETERMINATE] = "indeterminate",
2256 };
2257 DEFINE_STRING_TABLE_LOOKUP(dnssec_verdict, DnssecVerdict);