2 This file is part of systemd.
4 Copyright 2015 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
25 #include "dns-domain.h"
26 #include "gcrypt-util.h"
27 #include "hexdecoct.h"
28 #include "resolved-dns-dnssec.h"
29 #include "resolved-dns-packet.h"
30 #include "string-table.h"
32 #define VERIFY_RRS_MAX 256
33 #define MAX_KEY_SIZE (32*1024)
35 /* Permit a maximum clock skew of 1h 10min. This should be enough to deal with DST confusion */
36 #define SKEW_MAX (1*USEC_PER_HOUR + 10*USEC_PER_MINUTE)
38 /* Maximum number of NSEC3 iterations we'll do. RFC5155 says 2500 shall be the maximum useful value */
39 #define NSEC3_ITERATIONS_MAX 2500
42 * The DNSSEC Chain of trust:
44 * Normal RRs are protected via RRSIG RRs in combination with DNSKEY RRs, all in the same zone
45 * DNSKEY RRs are either protected like normal RRs, or via a DS from a zone "higher" up the tree
46 * DS RRs are protected like normal RRs
49 * Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS
52 uint16_t dnssec_keytag(DnsResourceRecord
*dnskey
, bool mask_revoke
) {
57 /* The algorithm from RFC 4034, Appendix B. */
60 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
62 f
= (uint32_t) dnskey
->dnskey
.flags
;
65 f
&= ~DNSKEY_FLAG_REVOKE
;
67 sum
= f
+ ((((uint32_t) dnskey
->dnskey
.protocol
) << 8) + (uint32_t) dnskey
->dnskey
.algorithm
);
69 p
= dnskey
->dnskey
.key
;
71 for (i
= 0; i
< dnskey
->dnskey
.key_size
; i
++)
72 sum
+= (i
& 1) == 0 ? (uint32_t) p
[i
] << 8 : (uint32_t) p
[i
];
74 sum
+= (sum
>> 16) & UINT32_C(0xFFFF);
76 return sum
& UINT32_C(0xFFFF);
79 int dnssec_canonicalize(const char *n
, char *buffer
, size_t buffer_max
) {
83 /* Converts the specified hostname into DNSSEC canonicalized
90 r
= dns_label_unescape(&n
, buffer
, buffer_max
);
96 if (buffer_max
< (size_t) r
+ 2)
99 /* The DNSSEC canonical form is not clear on what to
100 * do with dots appearing in labels, the way DNS-SD
101 * does it. Refuse it for now. */
103 if (memchr(buffer
, '.', r
))
106 ascii_strlower_n(buffer
, (size_t) r
);
116 /* Not even a single label: this is the root domain name */
118 assert(buffer_max
> 2);
130 static int rr_compare(const void *a
, const void *b
) {
131 DnsResourceRecord
**x
= (DnsResourceRecord
**) a
, **y
= (DnsResourceRecord
**) b
;
135 /* Let's order the RRs according to RFC 4034, Section 6.3 */
139 assert((*x
)->wire_format
);
142 assert((*y
)->wire_format
);
144 m
= MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(*x
), DNS_RESOURCE_RECORD_RDATA_SIZE(*y
));
146 r
= memcmp(DNS_RESOURCE_RECORD_RDATA(*x
), DNS_RESOURCE_RECORD_RDATA(*y
), m
);
150 if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x
) < DNS_RESOURCE_RECORD_RDATA_SIZE(*y
))
152 else if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x
) > DNS_RESOURCE_RECORD_RDATA_SIZE(*y
))
158 static int dnssec_rsa_verify_raw(
159 const char *hash_algorithm
,
160 const void *signature
, size_t signature_size
,
161 const void *data
, size_t data_size
,
162 const void *exponent
, size_t exponent_size
,
163 const void *modulus
, size_t modulus_size
) {
165 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
166 gcry_mpi_t n
= NULL
, e
= NULL
, s
= NULL
;
170 assert(hash_algorithm
);
172 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature
, signature_size
, NULL
);
178 ge
= gcry_mpi_scan(&e
, GCRYMPI_FMT_USG
, exponent
, exponent_size
, NULL
);
184 ge
= gcry_mpi_scan(&n
, GCRYMPI_FMT_USG
, modulus
, modulus_size
, NULL
);
190 ge
= gcry_sexp_build(&signature_sexp
,
192 "(sig-val (rsa (s %m)))",
200 ge
= gcry_sexp_build(&data_sexp
,
202 "(data (flags pkcs1) (hash %s %b))",
211 ge
= gcry_sexp_build(&public_key_sexp
,
213 "(public-key (rsa (n %m) (e %m)))",
221 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
222 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
225 log_debug("RSA signature check failed: %s", gpg_strerror(ge
));
239 gcry_sexp_release(public_key_sexp
);
241 gcry_sexp_release(signature_sexp
);
243 gcry_sexp_release(data_sexp
);
248 static int dnssec_rsa_verify(
249 const char *hash_algorithm
,
250 const void *hash
, size_t hash_size
,
251 DnsResourceRecord
*rrsig
,
252 DnsResourceRecord
*dnskey
) {
254 size_t exponent_size
, modulus_size
;
255 void *exponent
, *modulus
;
257 assert(hash_algorithm
);
259 assert(hash_size
> 0);
263 if (*(uint8_t*) dnskey
->dnskey
.key
== 0) {
264 /* exponent is > 255 bytes long */
266 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 3;
268 ((size_t) (((uint8_t*) dnskey
->dnskey
.key
)[1]) << 8) |
269 ((size_t) ((uint8_t*) dnskey
->dnskey
.key
)[2]);
271 if (exponent_size
< 256)
274 if (3 + exponent_size
>= dnskey
->dnskey
.key_size
)
277 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 3 + exponent_size
;
278 modulus_size
= dnskey
->dnskey
.key_size
- 3 - exponent_size
;
281 /* exponent is <= 255 bytes long */
283 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 1;
284 exponent_size
= (size_t) ((uint8_t*) dnskey
->dnskey
.key
)[0];
286 if (exponent_size
<= 0)
289 if (1 + exponent_size
>= dnskey
->dnskey
.key_size
)
292 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 1 + exponent_size
;
293 modulus_size
= dnskey
->dnskey
.key_size
- 1 - exponent_size
;
296 return dnssec_rsa_verify_raw(
298 rrsig
->rrsig
.signature
, rrsig
->rrsig
.signature_size
,
300 exponent
, exponent_size
,
301 modulus
, modulus_size
);
304 static int dnssec_ecdsa_verify_raw(
305 const char *hash_algorithm
,
307 const void *signature_r
, size_t signature_r_size
,
308 const void *signature_s
, size_t signature_s_size
,
309 const void *data
, size_t data_size
,
310 const void *key
, size_t key_size
) {
312 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
313 gcry_mpi_t q
= NULL
, r
= NULL
, s
= NULL
;
317 assert(hash_algorithm
);
319 ge
= gcry_mpi_scan(&r
, GCRYMPI_FMT_USG
, signature_r
, signature_r_size
, NULL
);
325 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature_s
, signature_s_size
, NULL
);
331 ge
= gcry_mpi_scan(&q
, GCRYMPI_FMT_USG
, key
, key_size
, NULL
);
337 ge
= gcry_sexp_build(&signature_sexp
,
339 "(sig-val (ecdsa (r %m) (s %m)))",
347 ge
= gcry_sexp_build(&data_sexp
,
349 "(data (flags rfc6979) (hash %s %b))",
358 ge
= gcry_sexp_build(&public_key_sexp
,
360 "(public-key (ecc (curve %s) (q %m)))",
368 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
369 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
372 log_debug("ECDSA signature check failed: %s", gpg_strerror(ge
));
385 gcry_sexp_release(public_key_sexp
);
387 gcry_sexp_release(signature_sexp
);
389 gcry_sexp_release(data_sexp
);
394 static int dnssec_ecdsa_verify(
395 const char *hash_algorithm
,
397 const void *hash
, size_t hash_size
,
398 DnsResourceRecord
*rrsig
,
399 DnsResourceRecord
*dnskey
) {
410 if (algorithm
== DNSSEC_ALGORITHM_ECDSAP256SHA256
) {
412 curve
= "NIST P-256";
413 } else if (algorithm
== DNSSEC_ALGORITHM_ECDSAP384SHA384
) {
415 curve
= "NIST P-384";
419 if (dnskey
->dnskey
.key_size
!= key_size
* 2)
422 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
425 q
= alloca(key_size
*2 + 1);
426 q
[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
427 memcpy(q
+1, dnskey
->dnskey
.key
, key_size
*2);
429 return dnssec_ecdsa_verify_raw(
432 rrsig
->rrsig
.signature
, key_size
,
433 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
438 static void md_add_uint8(gcry_md_hd_t md
, uint8_t v
) {
439 gcry_md_write(md
, &v
, sizeof(v
));
442 static void md_add_uint16(gcry_md_hd_t md
, uint16_t v
) {
444 gcry_md_write(md
, &v
, sizeof(v
));
447 static void md_add_uint32(gcry_md_hd_t md
, uint32_t v
) {
449 gcry_md_write(md
, &v
, sizeof(v
));
452 static int dnssec_rrsig_prepare(DnsResourceRecord
*rrsig
) {
453 int n_key_labels
, n_signer_labels
;
457 /* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source and
458 * .n_skip_labels_signer fields so that we can use them later on. */
461 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
463 /* Check if this RRSIG RR is already prepared */
464 if (rrsig
->n_skip_labels_source
!= (unsigned) -1)
467 if (rrsig
->rrsig
.inception
> rrsig
->rrsig
.expiration
)
470 name
= dns_resource_key_name(rrsig
->key
);
472 n_key_labels
= dns_name_count_labels(name
);
473 if (n_key_labels
< 0)
475 if (rrsig
->rrsig
.labels
> n_key_labels
)
478 n_signer_labels
= dns_name_count_labels(rrsig
->rrsig
.signer
);
479 if (n_signer_labels
< 0)
480 return n_signer_labels
;
481 if (n_signer_labels
> rrsig
->rrsig
.labels
)
484 r
= dns_name_skip(name
, n_key_labels
- n_signer_labels
, &name
);
490 /* Check if the signer is really a suffix of us */
491 r
= dns_name_equal(name
, rrsig
->rrsig
.signer
);
497 rrsig
->n_skip_labels_source
= n_key_labels
- rrsig
->rrsig
.labels
;
498 rrsig
->n_skip_labels_signer
= n_key_labels
- n_signer_labels
;
503 static int dnssec_rrsig_expired(DnsResourceRecord
*rrsig
, usec_t realtime
) {
504 usec_t expiration
, inception
, skew
;
507 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
509 if (realtime
== USEC_INFINITY
)
510 realtime
= now(CLOCK_REALTIME
);
512 expiration
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
513 inception
= rrsig
->rrsig
.inception
* USEC_PER_SEC
;
515 /* Consider inverted validity intervals as expired */
516 if (inception
> expiration
)
519 /* Permit a certain amount of clock skew of 10% of the valid
520 * time range. This takes inspiration from unbound's
522 skew
= (expiration
- inception
) / 10;
526 if (inception
< skew
)
531 if (expiration
+ skew
< expiration
)
532 expiration
= USEC_INFINITY
;
536 return realtime
< inception
|| realtime
> expiration
;
539 static int algorithm_to_gcrypt_md(uint8_t algorithm
) {
541 /* Translates a DNSSEC signature algorithm into a gcrypt
544 * Note that we implement all algorithms listed as "Must
545 * implement" and "Recommended to Implement" in RFC6944. We
546 * don't implement any algorithms that are listed as
547 * "Optional" or "Must Not Implement". Specifically, we do not
548 * implement RSAMD5, DSASHA1, DH, DSA-NSEC3-SHA1, and
553 case DNSSEC_ALGORITHM_RSASHA1
:
554 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
557 case DNSSEC_ALGORITHM_RSASHA256
:
558 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
559 return GCRY_MD_SHA256
;
561 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
562 return GCRY_MD_SHA384
;
564 case DNSSEC_ALGORITHM_RSASHA512
:
565 return GCRY_MD_SHA512
;
572 static void dnssec_fix_rrset_ttl(
573 DnsResourceRecord
*list
[],
575 DnsResourceRecord
*rrsig
,
584 for (k
= 0; k
< n
; k
++) {
585 DnsResourceRecord
*rr
= list
[k
];
587 /* Pick the TTL as the minimum of the RR's TTL, the
588 * RR's original TTL according to the RRSIG and the
589 * RRSIG's own TTL, see RFC 4035, Section 5.3.3 */
590 rr
->ttl
= MIN3(rr
->ttl
, rrsig
->rrsig
.original_ttl
, rrsig
->ttl
);
591 rr
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
593 /* Copy over information about the signer and wildcard source of synthesis */
594 rr
->n_skip_labels_source
= rrsig
->n_skip_labels_source
;
595 rr
->n_skip_labels_signer
= rrsig
->n_skip_labels_signer
;
598 rrsig
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
601 int dnssec_verify_rrset(
603 const DnsResourceKey
*key
,
604 DnsResourceRecord
*rrsig
,
605 DnsResourceRecord
*dnskey
,
607 DnssecResult
*result
) {
609 uint8_t wire_format_name
[DNS_WIRE_FOMAT_HOSTNAME_MAX
];
610 DnsResourceRecord
**list
, *rr
;
611 const char *source
, *name
;
612 gcry_md_hd_t md
= NULL
;
623 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
624 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
626 /* Verifies that the RRSet matches the specified "key" in "a",
627 * using the signature "rrsig" and the key "dnskey". It's
628 * assumed that RRSIG and DNSKEY match. */
630 md_algorithm
= algorithm_to_gcrypt_md(rrsig
->rrsig
.algorithm
);
631 if (md_algorithm
== -EOPNOTSUPP
) {
632 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
635 if (md_algorithm
< 0)
638 r
= dnssec_rrsig_prepare(rrsig
);
640 *result
= DNSSEC_INVALID
;
646 r
= dnssec_rrsig_expired(rrsig
, realtime
);
650 *result
= DNSSEC_SIGNATURE_EXPIRED
;
654 name
= dns_resource_key_name(key
);
656 /* Some keys may only appear signed in the zone apex, and are invalid anywhere else. (SOA, NS...) */
657 if (dns_type_apex_only(rrsig
->rrsig
.type_covered
)) {
658 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
662 *result
= DNSSEC_INVALID
;
667 /* OTOH DS RRs may not appear in the zone apex, but are valid everywhere else. */
668 if (rrsig
->rrsig
.type_covered
== DNS_TYPE_DS
) {
669 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
673 *result
= DNSSEC_INVALID
;
678 /* Determine the "Source of Synthesis" and whether this is a wildcard RRSIG */
679 r
= dns_name_suffix(name
, rrsig
->rrsig
.labels
, &source
);
682 if (r
> 0 && !dns_type_may_wildcard(rrsig
->rrsig
.type_covered
)) {
683 /* We refuse to validate NSEC3 or SOA RRs that are synthesized from wildcards */
684 *result
= DNSSEC_INVALID
;
688 /* If we stripped a single label, then let's see if that maybe was "*". If so, we are not really
689 * synthesized from a wildcard, we are the wildcard itself. Treat that like a normal name. */
690 r
= dns_name_startswith(name
, "*");
700 /* Collect all relevant RRs in a single array, so that we can look at the RRset */
701 list
= newa(DnsResourceRecord
*, dns_answer_size(a
));
703 DNS_ANSWER_FOREACH(rr
, a
) {
704 r
= dns_resource_key_equal(key
, rr
->key
);
710 /* We need the wire format for ordering, and digest calculation */
711 r
= dns_resource_record_to_wire_format(rr
, true);
717 if (n
> VERIFY_RRS_MAX
)
724 /* Bring the RRs into canonical order */
725 qsort_safe(list
, n
, sizeof(DnsResourceRecord
*), rr_compare
);
727 /* OK, the RRs are now in canonical order. Let's calculate the digest */
728 initialize_libgcrypt(false);
730 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
731 assert(hash_size
> 0);
733 gcry_md_open(&md
, md_algorithm
, 0);
737 md_add_uint16(md
, rrsig
->rrsig
.type_covered
);
738 md_add_uint8(md
, rrsig
->rrsig
.algorithm
);
739 md_add_uint8(md
, rrsig
->rrsig
.labels
);
740 md_add_uint32(md
, rrsig
->rrsig
.original_ttl
);
741 md_add_uint32(md
, rrsig
->rrsig
.expiration
);
742 md_add_uint32(md
, rrsig
->rrsig
.inception
);
743 md_add_uint16(md
, rrsig
->rrsig
.key_tag
);
745 r
= dns_name_to_wire_format(rrsig
->rrsig
.signer
, wire_format_name
, sizeof(wire_format_name
), true);
748 gcry_md_write(md
, wire_format_name
, r
);
750 /* Convert the source of synthesis into wire format */
751 r
= dns_name_to_wire_format(source
, wire_format_name
, sizeof(wire_format_name
), true);
755 for (k
= 0; k
< n
; k
++) {
760 /* Hash the source of synthesis. If this is a wildcard, then prefix it with the *. label */
762 gcry_md_write(md
, (uint8_t[]) { 1, '*'}, 2);
763 gcry_md_write(md
, wire_format_name
, r
);
765 md_add_uint16(md
, rr
->key
->type
);
766 md_add_uint16(md
, rr
->key
->class);
767 md_add_uint32(md
, rrsig
->rrsig
.original_ttl
);
769 l
= DNS_RESOURCE_RECORD_RDATA_SIZE(rr
);
772 md_add_uint16(md
, (uint16_t) l
);
773 gcry_md_write(md
, DNS_RESOURCE_RECORD_RDATA(rr
), l
);
776 hash
= gcry_md_read(md
, 0);
782 switch (rrsig
->rrsig
.algorithm
) {
784 case DNSSEC_ALGORITHM_RSASHA1
:
785 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
786 case DNSSEC_ALGORITHM_RSASHA256
:
787 case DNSSEC_ALGORITHM_RSASHA512
:
788 r
= dnssec_rsa_verify(
789 gcry_md_algo_name(md_algorithm
),
795 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
796 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
797 r
= dnssec_ecdsa_verify(
798 gcry_md_algo_name(md_algorithm
),
799 rrsig
->rrsig
.algorithm
,
809 /* Now, fix the ttl, expiry, and remember the synthesizing source and the signer */
811 dnssec_fix_rrset_ttl(list
, n
, rrsig
, realtime
);
814 *result
= DNSSEC_INVALID
;
816 *result
= DNSSEC_VALIDATED_WILDCARD
;
818 *result
= DNSSEC_VALIDATED
;
827 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
832 /* Checks if the specified DNSKEY RR matches the key used for
833 * the signature in the specified RRSIG RR */
835 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
838 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
840 if (dnskey
->key
->class != rrsig
->key
->class)
842 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
844 if (!revoked_ok
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
846 if (dnskey
->dnskey
.protocol
!= 3)
848 if (dnskey
->dnskey
.algorithm
!= rrsig
->rrsig
.algorithm
)
851 if (dnssec_keytag(dnskey
, false) != rrsig
->rrsig
.key_tag
)
854 return dns_name_equal(dns_resource_key_name(dnskey
->key
), rrsig
->rrsig
.signer
);
857 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
861 /* Checks if the specified RRSIG RR protects the RRSet of the specified RR key. */
863 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
865 if (rrsig
->key
->class != key
->class)
867 if (rrsig
->rrsig
.type_covered
!= key
->type
)
870 return dns_name_equal(dns_resource_key_name(rrsig
->key
), dns_resource_key_name(key
));
873 int dnssec_verify_rrset_search(
875 const DnsResourceKey
*key
,
876 DnsAnswer
*validated_dnskeys
,
878 DnssecResult
*result
,
879 DnsResourceRecord
**ret_rrsig
) {
881 bool found_rrsig
= false, found_invalid
= false, found_expired_rrsig
= false, found_unsupported_algorithm
= false;
882 DnsResourceRecord
*rrsig
;
888 /* Verifies all RRs from "a" that match the key "key" against DNSKEYs in "validated_dnskeys" */
890 if (!a
|| a
->n_rrs
<= 0)
893 /* Iterate through each RRSIG RR. */
894 DNS_ANSWER_FOREACH(rrsig
, a
) {
895 DnsResourceRecord
*dnskey
;
896 DnsAnswerFlags flags
;
898 /* Is this an RRSIG RR that applies to RRs matching our key? */
899 r
= dnssec_key_match_rrsig(key
, rrsig
);
907 /* Look for a matching key */
908 DNS_ANSWER_FOREACH_FLAGS(dnskey
, flags
, validated_dnskeys
) {
909 DnssecResult one_result
;
911 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
914 /* Is this a DNSKEY RR that matches they key of our RRSIG? */
915 r
= dnssec_rrsig_match_dnskey(rrsig
, dnskey
, false);
921 /* Take the time here, if it isn't set yet, so
922 * that we do all validations with the same
924 if (realtime
== USEC_INFINITY
)
925 realtime
= now(CLOCK_REALTIME
);
927 /* Yay, we found a matching RRSIG with a matching
928 * DNSKEY, awesome. Now let's verify all entries of
929 * the RRSet against the RRSIG and DNSKEY
932 r
= dnssec_verify_rrset(a
, key
, rrsig
, dnskey
, realtime
, &one_result
);
936 switch (one_result
) {
938 case DNSSEC_VALIDATED
:
939 case DNSSEC_VALIDATED_WILDCARD
:
940 /* Yay, the RR has been validated,
941 * return immediately, but fix up the expiry */
945 *result
= one_result
;
949 /* If the signature is invalid, let's try another
950 key and/or signature. After all they
951 key_tags and stuff are not unique, and
952 might be shared by multiple keys. */
953 found_invalid
= true;
956 case DNSSEC_UNSUPPORTED_ALGORITHM
:
957 /* If the key algorithm is
958 unsupported, try another
959 RRSIG/DNSKEY pair, but remember we
960 encountered this, so that we can
961 return a proper error when we
962 encounter nothing better. */
963 found_unsupported_algorithm
= true;
966 case DNSSEC_SIGNATURE_EXPIRED
:
967 /* If the signature is expired, try
968 another one, but remember it, so
969 that we can return this */
970 found_expired_rrsig
= true;
974 assert_not_reached("Unexpected DNSSEC validation result");
979 if (found_expired_rrsig
)
980 *result
= DNSSEC_SIGNATURE_EXPIRED
;
981 else if (found_unsupported_algorithm
)
982 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
983 else if (found_invalid
)
984 *result
= DNSSEC_INVALID
;
985 else if (found_rrsig
)
986 *result
= DNSSEC_MISSING_KEY
;
988 *result
= DNSSEC_NO_SIGNATURE
;
996 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
997 DnsResourceRecord
*rr
;
1000 /* Checks whether there's at least one RRSIG in 'a' that proctects RRs of the specified key */
1002 DNS_ANSWER_FOREACH(rr
, a
) {
1003 r
= dnssec_key_match_rrsig(key
, rr
);
1013 static int digest_to_gcrypt_md(uint8_t algorithm
) {
1015 /* Translates a DNSSEC digest algorithm into a gcrypt digest identifier */
1017 switch (algorithm
) {
1019 case DNSSEC_DIGEST_SHA1
:
1020 return GCRY_MD_SHA1
;
1022 case DNSSEC_DIGEST_SHA256
:
1023 return GCRY_MD_SHA256
;
1025 case DNSSEC_DIGEST_SHA384
:
1026 return GCRY_MD_SHA384
;
1033 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
1034 char owner_name
[DNSSEC_CANONICAL_HOSTNAME_MAX
];
1035 gcry_md_hd_t md
= NULL
;
1037 int md_algorithm
, r
;
1043 /* Implements DNSKEY verification by a DS, according to RFC 4035, section 5.2 */
1045 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1047 if (ds
->key
->type
!= DNS_TYPE_DS
)
1049 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
1050 return -EKEYREJECTED
;
1051 if (!mask_revoke
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
1052 return -EKEYREJECTED
;
1053 if (dnskey
->dnskey
.protocol
!= 3)
1054 return -EKEYREJECTED
;
1056 if (dnskey
->dnskey
.algorithm
!= ds
->ds
.algorithm
)
1058 if (dnssec_keytag(dnskey
, mask_revoke
) != ds
->ds
.key_tag
)
1061 initialize_libgcrypt(false);
1063 md_algorithm
= digest_to_gcrypt_md(ds
->ds
.digest_type
);
1064 if (md_algorithm
< 0)
1065 return md_algorithm
;
1067 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
1068 assert(hash_size
> 0);
1070 if (ds
->ds
.digest_size
!= hash_size
)
1073 r
= dnssec_canonicalize(dns_resource_key_name(dnskey
->key
), owner_name
, sizeof(owner_name
));
1077 gcry_md_open(&md
, md_algorithm
, 0);
1081 gcry_md_write(md
, owner_name
, r
);
1083 md_add_uint16(md
, dnskey
->dnskey
.flags
& ~DNSKEY_FLAG_REVOKE
);
1085 md_add_uint16(md
, dnskey
->dnskey
.flags
);
1086 md_add_uint8(md
, dnskey
->dnskey
.protocol
);
1087 md_add_uint8(md
, dnskey
->dnskey
.algorithm
);
1088 gcry_md_write(md
, dnskey
->dnskey
.key
, dnskey
->dnskey
.key_size
);
1090 result
= gcry_md_read(md
, 0);
1096 r
= memcmp(result
, ds
->ds
.digest
, ds
->ds
.digest_size
) != 0;
1103 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
1104 DnsResourceRecord
*ds
;
1105 DnsAnswerFlags flags
;
1110 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1113 DNS_ANSWER_FOREACH_FLAGS(ds
, flags
, validated_ds
) {
1115 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1118 if (ds
->key
->type
!= DNS_TYPE_DS
)
1120 if (ds
->key
->class != dnskey
->key
->class)
1123 r
= dns_name_equal(dns_resource_key_name(dnskey
->key
), dns_resource_key_name(ds
->key
));
1129 r
= dnssec_verify_dnskey_by_ds(dnskey
, ds
, false);
1130 if (IN_SET(r
, -EKEYREJECTED
, -EOPNOTSUPP
))
1131 return 0; /* The DNSKEY is revoked or otherwise invalid, or we don't support the digest algorithm */
1141 static int nsec3_hash_to_gcrypt_md(uint8_t algorithm
) {
1143 /* Translates a DNSSEC NSEC3 hash algorithm into a gcrypt digest identifier */
1145 switch (algorithm
) {
1147 case NSEC3_ALGORITHM_SHA1
:
1148 return GCRY_MD_SHA1
;
1155 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
1156 uint8_t wire_format
[DNS_WIRE_FOMAT_HOSTNAME_MAX
];
1157 gcry_md_hd_t md
= NULL
;
1168 if (nsec3
->key
->type
!= DNS_TYPE_NSEC3
)
1171 if (nsec3
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
) {
1172 log_debug("Ignoring NSEC3 RR %s with excessive number of iterations.", dns_resource_record_to_string(nsec3
));
1176 algorithm
= nsec3_hash_to_gcrypt_md(nsec3
->nsec3
.algorithm
);
1180 initialize_libgcrypt(false);
1182 hash_size
= gcry_md_get_algo_dlen(algorithm
);
1183 assert(hash_size
> 0);
1185 if (nsec3
->nsec3
.next_hashed_name_size
!= hash_size
)
1188 r
= dns_name_to_wire_format(name
, wire_format
, sizeof(wire_format
), true);
1192 gcry_md_open(&md
, algorithm
, 0);
1196 gcry_md_write(md
, wire_format
, r
);
1197 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1199 result
= gcry_md_read(md
, 0);
1205 for (k
= 0; k
< nsec3
->nsec3
.iterations
; k
++) {
1206 uint8_t tmp
[hash_size
];
1207 memcpy(tmp
, result
, hash_size
);
1210 gcry_md_write(md
, tmp
, hash_size
);
1211 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1213 result
= gcry_md_read(md
, 0);
1220 memcpy(ret
, result
, hash_size
);
1221 r
= (int) hash_size
;
1228 static int nsec3_is_good(DnsResourceRecord
*rr
, DnsResourceRecord
*nsec3
) {
1234 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
1237 /* RFC 5155, Section 8.2 says we MUST ignore NSEC3 RRs with flags != 0 or 1 */
1238 if (!IN_SET(rr
->nsec3
.flags
, 0, 1))
1241 /* Ignore NSEC3 RRs whose algorithm we don't know */
1242 if (nsec3_hash_to_gcrypt_md(rr
->nsec3
.algorithm
) < 0)
1244 /* Ignore NSEC3 RRs with an excessive number of required iterations */
1245 if (rr
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1248 /* Ignore NSEC3 RRs generated from wildcards. If these NSEC3 RRs weren't correctly signed we can't make this
1249 * check (since rr->n_skip_labels_source is -1), but that's OK, as we won't trust them anyway in that case. */
1250 if (rr
->n_skip_labels_source
!= 0 && rr
->n_skip_labels_source
!= (unsigned) -1)
1252 /* Ignore NSEC3 RRs that are located anywhere else than one label below the zone */
1253 if (rr
->n_skip_labels_signer
!= 1 && rr
->n_skip_labels_signer
!= (unsigned) -1)
1259 /* If a second NSEC3 RR is specified, also check if they are from the same zone. */
1261 if (nsec3
== rr
) /* Shortcut */
1264 if (rr
->key
->class != nsec3
->key
->class)
1266 if (rr
->nsec3
.algorithm
!= nsec3
->nsec3
.algorithm
)
1268 if (rr
->nsec3
.iterations
!= nsec3
->nsec3
.iterations
)
1270 if (rr
->nsec3
.salt_size
!= nsec3
->nsec3
.salt_size
)
1272 if (memcmp(rr
->nsec3
.salt
, nsec3
->nsec3
.salt
, rr
->nsec3
.salt_size
) != 0)
1275 a
= dns_resource_key_name(rr
->key
);
1276 r
= dns_name_parent(&a
); /* strip off hash */
1282 b
= dns_resource_key_name(nsec3
->key
);
1283 r
= dns_name_parent(&b
); /* strip off hash */
1289 /* Make sure both have the same parent */
1290 return dns_name_equal(a
, b
);
1293 static int nsec3_hashed_domain_format(const uint8_t *hashed
, size_t hashed_size
, const char *zone
, char **ret
) {
1294 _cleanup_free_
char *l
= NULL
;
1298 assert(hashed_size
> 0);
1302 l
= base32hexmem(hashed
, hashed_size
, false);
1306 j
= strjoin(l
, ".", zone
, NULL
);
1311 return (int) hashed_size
;
1314 static int nsec3_hashed_domain_make(DnsResourceRecord
*nsec3
, const char *domain
, const char *zone
, char **ret
) {
1315 uint8_t hashed
[DNSSEC_HASH_SIZE_MAX
];
1323 hashed_size
= dnssec_nsec3_hash(nsec3
, domain
, hashed
);
1324 if (hashed_size
< 0)
1327 return nsec3_hashed_domain_format(hashed
, (size_t) hashed_size
, zone
, ret
);
1330 /* See RFC 5155, Section 8
1331 * First try to find a NSEC3 record that matches our query precisely, if that fails, find the closest
1332 * enclosure. Secondly, find a proof that there is no closer enclosure and either a proof that there
1333 * is no wildcard domain as a direct descendant of the closest enclosure, or find an NSEC3 record that
1334 * matches the wildcard domain.
1336 * Based on this we can prove either the existence of the record in @key, or NXDOMAIN or NODATA, or
1337 * that there is no proof either way. The latter is the case if a the proof of non-existence of a given
1338 * name uses an NSEC3 record with the opt-out bit set. Lastly, if we are given insufficient NSEC3 records
1339 * to conclude anything we indicate this by returning NO_RR. */
1340 static int dnssec_test_nsec3(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1341 _cleanup_free_
char *next_closer_domain
= NULL
, *wildcard_domain
= NULL
;
1342 const char *zone
, *p
, *pp
= NULL
, *wildcard
;
1343 DnsResourceRecord
*rr
, *enclosure_rr
, *zone_rr
, *wildcard_rr
= NULL
;
1344 DnsAnswerFlags flags
;
1346 bool a
, no_closer
= false, no_wildcard
= false, optout
= false;
1351 /* First step, find the zone name and the NSEC3 parameters of the zone.
1352 * it is sufficient to look for the longest common suffix we find with
1353 * any NSEC3 RR in the response. Any NSEC3 record will do as all NSEC3
1354 * records from a given zone in a response must use the same
1356 zone
= dns_resource_key_name(key
);
1358 DNS_ANSWER_FOREACH_FLAGS(zone_rr
, flags
, answer
) {
1359 r
= nsec3_is_good(zone_rr
, NULL
);
1365 r
= dns_name_equal_skip(dns_resource_key_name(zone_rr
->key
), 1, zone
);
1372 /* Strip one label from the front */
1373 r
= dns_name_parent(&zone
);
1380 *result
= DNSSEC_NSEC_NO_RR
;
1384 /* Second step, find the closest encloser NSEC3 RR in 'answer' that matches 'key' */
1385 p
= dns_resource_key_name(key
);
1387 _cleanup_free_
char *hashed_domain
= NULL
;
1389 hashed_size
= nsec3_hashed_domain_make(zone_rr
, p
, zone
, &hashed_domain
);
1390 if (hashed_size
== -EOPNOTSUPP
) {
1391 *result
= DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
;
1394 if (hashed_size
< 0)
1397 DNS_ANSWER_FOREACH_FLAGS(enclosure_rr
, flags
, answer
) {
1399 r
= nsec3_is_good(enclosure_rr
, zone_rr
);
1405 if (enclosure_rr
->nsec3
.next_hashed_name_size
!= (size_t) hashed_size
)
1408 r
= dns_name_equal(dns_resource_key_name(enclosure_rr
->key
), hashed_domain
);
1412 a
= flags
& DNS_ANSWER_AUTHENTICATED
;
1413 goto found_closest_encloser
;
1417 /* We didn't find the closest encloser with this name,
1418 * but let's remember this domain name, it might be
1419 * the next closer name */
1423 /* Strip one label from the front */
1424 r
= dns_name_parent(&p
);
1431 *result
= DNSSEC_NSEC_NO_RR
;
1434 found_closest_encloser
:
1435 /* We found a closest encloser in 'p'; next closer is 'pp' */
1438 /* We have an exact match! If we area looking for a DS RR, then we must insist that we got the NSEC3 RR
1439 * from the parent. Otherwise the one from the child. Do so, by checking whether SOA and NS are
1440 * appropriately set. */
1442 if (key
->type
== DNS_TYPE_DS
) {
1443 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1446 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1447 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1451 /* No next closer NSEC3 RR. That means there's a direct NSEC3 RR for our key. */
1452 if (bitmap_isset(enclosure_rr
->nsec3
.types
, key
->type
))
1453 *result
= DNSSEC_NSEC_FOUND
;
1454 else if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1455 *result
= DNSSEC_NSEC_CNAME
;
1457 *result
= DNSSEC_NSEC_NODATA
;
1462 *ttl
= enclosure_rr
->ttl
;
1467 /* Ensure this is not a DNAME domain, see RFC5155, section 8.3. */
1468 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_DNAME
))
1471 /* Ensure that this data is from the delegated domain
1472 * (i.e. originates from the "lower" DNS server), and isn't
1473 * just glue records (i.e. doesn't originate from the "upper"
1475 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1476 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1479 /* Prove that there is no next closer and whether or not there is a wildcard domain. */
1481 wildcard
= strjoina("*.", p
);
1482 r
= nsec3_hashed_domain_make(enclosure_rr
, wildcard
, zone
, &wildcard_domain
);
1485 if (r
!= hashed_size
)
1488 r
= nsec3_hashed_domain_make(enclosure_rr
, pp
, zone
, &next_closer_domain
);
1491 if (r
!= hashed_size
)
1494 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1495 _cleanup_free_
char *next_hashed_domain
= NULL
;
1497 r
= nsec3_is_good(rr
, zone_rr
);
1503 r
= nsec3_hashed_domain_format(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, zone
, &next_hashed_domain
);
1507 r
= dns_name_between(dns_resource_key_name(rr
->key
), next_closer_domain
, next_hashed_domain
);
1511 if (rr
->nsec3
.flags
& 1)
1514 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1519 r
= dns_name_equal(dns_resource_key_name(rr
->key
), wildcard_domain
);
1523 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1528 r
= dns_name_between(dns_resource_key_name(rr
->key
), wildcard_domain
, next_hashed_domain
);
1532 if (rr
->nsec3
.flags
& 1)
1533 /* This only makes sense if we have a wildcard delegation, which is
1534 * very unlikely, see RFC 4592, Section 4.2, but we cannot rely on
1535 * this not happening, so hence cannot simply conclude NXDOMAIN as
1539 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1545 if (wildcard_rr
&& no_wildcard
)
1549 *result
= DNSSEC_NSEC_NO_RR
;
1554 /* A wildcard exists that matches our query. */
1556 /* This is not specified in any RFC to the best of my knowledge, but
1557 * if the next closer enclosure is covered by an opt-out NSEC3 RR
1558 * it means that we cannot prove that the source of synthesis is
1559 * correct, as there may be a closer match. */
1560 *result
= DNSSEC_NSEC_OPTOUT
;
1561 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, key
->type
))
1562 *result
= DNSSEC_NSEC_FOUND
;
1563 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1564 *result
= DNSSEC_NSEC_CNAME
;
1566 *result
= DNSSEC_NSEC_NODATA
;
1569 /* The RFC only specifies that we have to care for optout for NODATA for
1570 * DS records. However, children of an insecure opt-out delegation should
1571 * also be considered opt-out, rather than verified NXDOMAIN.
1572 * Note that we do not require a proof of wildcard non-existence if the
1573 * next closer domain is covered by an opt-out, as that would not provide
1574 * any additional information. */
1575 *result
= DNSSEC_NSEC_OPTOUT
;
1576 else if (no_wildcard
)
1577 *result
= DNSSEC_NSEC_NXDOMAIN
;
1579 *result
= DNSSEC_NSEC_NO_RR
;
1589 *ttl
= enclosure_rr
->ttl
;
1594 static int dnssec_nsec_wildcard_equal(DnsResourceRecord
*rr
, const char *name
) {
1595 char label
[DNS_LABEL_MAX
];
1600 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1602 /* Checks whether the specified RR has a name beginning in "*.", and if the rest is a suffix of our name */
1604 if (rr
->n_skip_labels_source
!= 1)
1607 n
= dns_resource_key_name(rr
->key
);
1608 r
= dns_label_unescape(&n
, label
, sizeof(label
));
1611 if (r
!= 1 || label
[0] != '*')
1614 return dns_name_endswith(name
, n
);
1617 static int dnssec_nsec_in_path(DnsResourceRecord
*rr
, const char *name
) {
1618 const char *nn
, *common_suffix
;
1622 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1624 /* Checks whether the specified nsec RR indicates that name is an empty non-terminal (ENT)
1626 * A couple of examples:
1628 * NSEC bar → waldo.foo.bar: indicates that foo.bar exists and is an ENT
1629 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that xoo.bar and zzz.xoo.bar exist and are ENTs
1630 * NSEC yyy.zzz.xoo.bar → bar: indicates pretty much nothing about ENTs
1633 /* First, determine parent of next domain. */
1634 nn
= rr
->nsec
.next_domain_name
;
1635 r
= dns_name_parent(&nn
);
1639 /* If the name we just determined is not equal or child of the name we are interested in, then we can't say
1640 * anything at all. */
1641 r
= dns_name_endswith(nn
, name
);
1645 /* 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. */
1646 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
1650 return dns_name_endswith(name
, common_suffix
);
1653 static int dnssec_nsec_from_parent_zone(DnsResourceRecord
*rr
, const char *name
) {
1657 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1659 /* Checks whether this NSEC originates to the parent zone or the child zone. */
1661 r
= dns_name_parent(&name
);
1665 r
= dns_name_equal(name
, dns_resource_key_name(rr
->key
));
1669 /* DNAME, and NS without SOA is an indication for a delegation. */
1670 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_DNAME
))
1673 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) && !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1679 static int dnssec_nsec_covers(DnsResourceRecord
*rr
, const char *name
) {
1680 const char *common_suffix
, *p
;
1684 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1686 /* Checks whether the "Next Closer" is witin the space covered by the specified RR. */
1688 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
1694 r
= dns_name_parent(&name
);
1700 r
= dns_name_equal(name
, common_suffix
);
1707 /* p is now the "Next Closer". */
1709 return dns_name_between(dns_resource_key_name(rr
->key
), p
, rr
->nsec
.next_domain_name
);
1712 static int dnssec_nsec_covers_wildcard(DnsResourceRecord
*rr
, const char *name
) {
1713 const char *common_suffix
, *wc
;
1717 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1719 /* Checks whether the "Wildcard at the Closest Encloser" is within the space covered by the specified
1720 * RR. Specifically, checks whether 'name' has the common suffix of the NSEC RR's owner and next names as
1721 * suffix, and whether the NSEC covers the name generated by that suffix prepended with an asterisk label.
1723 * NSEC bar → waldo.foo.bar: indicates that *.bar and *.foo.bar do not exist
1724 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that *.xoo.bar and *.zzz.xoo.bar do not exist (and more ...)
1725 * NSEC yyy.zzz.xoo.bar → bar: indicates that a number of wildcards don#t exist either...
1728 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
1732 /* If the common suffix is not shared by the name we are interested in, it has nothing to say for us. */
1733 r
= dns_name_endswith(name
, common_suffix
);
1737 wc
= strjoina("*.", common_suffix
);
1738 return dns_name_between(dns_resource_key_name(rr
->key
), wc
, rr
->nsec
.next_domain_name
);
1741 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1742 bool have_nsec3
= false, covering_rr_authenticated
= false, wildcard_rr_authenticated
= false;
1743 DnsResourceRecord
*rr
, *covering_rr
= NULL
, *wildcard_rr
= NULL
;
1744 DnsAnswerFlags flags
;
1751 /* Look for any NSEC/NSEC3 RRs that say something about the specified key. */
1753 name
= dns_resource_key_name(key
);
1755 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1757 if (rr
->key
->class != key
->class)
1760 have_nsec3
= have_nsec3
|| (rr
->key
->type
== DNS_TYPE_NSEC3
);
1762 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
1765 /* The following checks only make sense for NSEC RRs that are not expanded from a wildcard */
1766 r
= dns_resource_record_is_synthetic(rr
);
1772 /* Check if this is a direct match. If so, we have encountered a NODATA case */
1773 r
= dns_name_equal(dns_resource_key_name(rr
->key
), name
);
1777 /* If it's not a direct match, maybe it's a wild card match? */
1778 r
= dnssec_nsec_wildcard_equal(rr
, name
);
1783 if (key
->type
== DNS_TYPE_DS
) {
1784 /* If we look for a DS RR and the server sent us the NSEC RR of the child zone
1785 * we have a problem. For DS RRs we want the NSEC RR from the parent */
1786 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1789 /* For all RR types, ensure that if NS is set SOA is set too, so that we know
1790 * we got the child's NSEC. */
1791 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) &&
1792 !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1796 if (bitmap_isset(rr
->nsec
.types
, key
->type
))
1797 *result
= DNSSEC_NSEC_FOUND
;
1798 else if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_CNAME
))
1799 *result
= DNSSEC_NSEC_CNAME
;
1801 *result
= DNSSEC_NSEC_NODATA
;
1804 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1811 /* Check if the name we are looking for is an empty non-terminal within the owner or next name
1812 * of the NSEC RR. */
1813 r
= dnssec_nsec_in_path(rr
, name
);
1817 *result
= DNSSEC_NSEC_NODATA
;
1820 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1827 /* The following two "covering" checks, are not useful if the NSEC is from the parent */
1828 r
= dnssec_nsec_from_parent_zone(rr
, name
);
1834 /* Check if this NSEC RR proves the absence of an explicit RR under this name */
1835 r
= dnssec_nsec_covers(rr
, name
);
1838 if (r
> 0 && (!covering_rr
|| !covering_rr_authenticated
)) {
1840 covering_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1843 /* Check if this NSEC RR proves the absence of a wildcard RR under this name */
1844 r
= dnssec_nsec_covers_wildcard(rr
, name
);
1847 if (r
> 0 && (!wildcard_rr
|| !wildcard_rr_authenticated
)) {
1849 wildcard_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1853 if (covering_rr
&& wildcard_rr
) {
1854 /* If we could prove that neither the name itself, nor the wildcard at the closest encloser exists, we
1855 * proved the NXDOMAIN case. */
1856 *result
= DNSSEC_NSEC_NXDOMAIN
;
1859 *authenticated
= covering_rr_authenticated
&& wildcard_rr_authenticated
;
1861 *ttl
= MIN(covering_rr
->ttl
, wildcard_rr
->ttl
);
1866 /* OK, this was not sufficient. Let's see if NSEC3 can help. */
1868 return dnssec_test_nsec3(answer
, key
, result
, authenticated
, ttl
);
1870 /* No approproate NSEC RR found, report this. */
1871 *result
= DNSSEC_NSEC_NO_RR
;
1875 static int dnssec_nsec_test_enclosed(DnsAnswer
*answer
, uint16_t type
, const char *name
, const char *zone
, bool *authenticated
) {
1876 DnsResourceRecord
*rr
;
1877 DnsAnswerFlags flags
;
1883 /* Checks whether there's an NSEC/NSEC3 that proves that the specified 'name' is non-existing in the specified
1884 * 'zone'. The 'zone' must be a suffix of the 'name'. */
1886 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1889 if (rr
->key
->type
!= type
&& type
!= DNS_TYPE_ANY
)
1892 switch (rr
->key
->type
) {
1896 /* We only care for NSEC RRs from the indicated zone */
1897 r
= dns_resource_record_is_signer(rr
, zone
);
1903 r
= dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
1910 case DNS_TYPE_NSEC3
: {
1911 _cleanup_free_
char *hashed_domain
= NULL
, *next_hashed_domain
= NULL
;
1913 /* We only care for NSEC3 RRs from the indicated zone */
1914 r
= dns_resource_record_is_signer(rr
, zone
);
1920 r
= nsec3_is_good(rr
, NULL
);
1926 /* Format the domain we are testing with the NSEC3 RR's hash function */
1927 r
= nsec3_hashed_domain_make(
1934 if ((size_t) r
!= rr
->nsec3
.next_hashed_name_size
)
1937 /* Format the NSEC3's next hashed name as proper domain name */
1938 r
= nsec3_hashed_domain_format(
1939 rr
->nsec3
.next_hashed_name
,
1940 rr
->nsec3
.next_hashed_name_size
,
1942 &next_hashed_domain
);
1946 r
= dns_name_between(dns_resource_key_name(rr
->key
), hashed_domain
, next_hashed_domain
);
1960 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1968 static int dnssec_test_positive_wildcard_nsec3(
1973 bool *authenticated
) {
1975 const char *next_closer
= NULL
;
1978 /* Run a positive NSEC3 wildcard proof. Specifically:
1980 * A proof that the "next closer" of the generating wildcard does not exist.
1982 * Note a key difference between the NSEC3 and NSEC versions of the proof. NSEC RRs don't have to exist for
1983 * empty non-transients. NSEC3 RRs however have to. This means it's sufficient to check if the next closer name
1984 * exists for the NSEC3 RR and we are done.
1986 * 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
1987 * c.d.e.f does not exist. */
1991 r
= dns_name_parent(&name
);
1997 r
= dns_name_equal(name
, source
);
2004 return dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC3
, next_closer
, zone
, authenticated
);
2007 static int dnssec_test_positive_wildcard_nsec(
2012 bool *_authenticated
) {
2014 bool authenticated
= true;
2017 /* Run a positive NSEC wildcard proof. Specifically:
2019 * A proof that there's neither a wildcard name nor a non-wildcard name that is a suffix of the name "name" and
2020 * a prefix of the synthesizing source "source" in the zone "zone".
2022 * See RFC 5155, Section 8.8 and RFC 4035, Section 5.3.4
2024 * 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
2025 * have to prove that none of the following exist:
2036 _cleanup_free_
char *wc
= NULL
;
2039 /* Check if there's an NSEC or NSEC3 RR that proves that the mame we determined is really non-existing,
2040 * i.e between the owner name and the next name of an NSEC RR. */
2041 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, name
, zone
, &a
);
2045 authenticated
= authenticated
&& a
;
2047 /* Strip one label off */
2048 r
= dns_name_parent(&name
);
2052 /* Did we reach the source of synthesis? */
2053 r
= dns_name_equal(name
, source
);
2057 /* Successful exit */
2058 *_authenticated
= authenticated
;
2062 /* Safety check, that the source of synthesis is still our suffix */
2063 r
= dns_name_endswith(name
, source
);
2069 /* Replace the label we stripped off with an asterisk */
2070 wc
= strappend("*.", name
);
2074 /* And check if the proof holds for the asterisk name, too */
2075 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, wc
, zone
, &a
);
2079 authenticated
= authenticated
&& a
;
2080 /* In the next iteration we'll check the non-asterisk-prefixed version */
2084 int dnssec_test_positive_wildcard(
2089 bool *authenticated
) {
2096 assert(authenticated
);
2098 r
= dns_answer_contains_zone_nsec3(answer
, zone
);
2102 return dnssec_test_positive_wildcard_nsec3(answer
, name
, source
, zone
, authenticated
);
2104 return dnssec_test_positive_wildcard_nsec(answer
, name
, source
, zone
, authenticated
);
2109 int dnssec_verify_rrset(
2111 const DnsResourceKey
*key
,
2112 DnsResourceRecord
*rrsig
,
2113 DnsResourceRecord
*dnskey
,
2115 DnssecResult
*result
) {
2120 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
2125 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
2130 int dnssec_verify_rrset_search(
2132 const DnsResourceKey
*key
,
2133 DnsAnswer
*validated_dnskeys
,
2135 DnssecResult
*result
,
2136 DnsResourceRecord
**ret_rrsig
) {
2141 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
2146 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
2151 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
2156 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
2161 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
2166 int dnssec_test_positive_wildcard(
2171 bool *authenticated
) {
2178 static const char* const dnssec_result_table
[_DNSSEC_RESULT_MAX
] = {
2179 [DNSSEC_VALIDATED
] = "validated",
2180 [DNSSEC_VALIDATED_WILDCARD
] = "validated-wildcard",
2181 [DNSSEC_INVALID
] = "invalid",
2182 [DNSSEC_SIGNATURE_EXPIRED
] = "signature-expired",
2183 [DNSSEC_UNSUPPORTED_ALGORITHM
] = "unsupported-algorithm",
2184 [DNSSEC_NO_SIGNATURE
] = "no-signature",
2185 [DNSSEC_MISSING_KEY
] = "missing-key",
2186 [DNSSEC_UNSIGNED
] = "unsigned",
2187 [DNSSEC_FAILED_AUXILIARY
] = "failed-auxiliary",
2188 [DNSSEC_NSEC_MISMATCH
] = "nsec-mismatch",
2189 [DNSSEC_INCOMPATIBLE_SERVER
] = "incompatible-server",
2191 DEFINE_STRING_TABLE_LOOKUP(dnssec_result
, DnssecResult
);
2193 static const char* const dnssec_verdict_table
[_DNSSEC_VERDICT_MAX
] = {
2194 [DNSSEC_SECURE
] = "secure",
2195 [DNSSEC_INSECURE
] = "insecure",
2196 [DNSSEC_BOGUS
] = "bogus",
2197 [DNSSEC_INDETERMINATE
] = "indeterminate",
2199 DEFINE_STRING_TABLE_LOOKUP(dnssec_verdict
, DnssecVerdict
);