1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2015 Lennart Poettering
12 #include "alloc-util.h"
13 #include "dns-domain.h"
16 #include "gcrypt-util.h"
17 #include "hexdecoct.h"
18 #include "resolved-dns-dnssec.h"
19 #include "resolved-dns-packet.h"
20 #include "string-table.h"
22 #define VERIFY_RRS_MAX 256
23 #define MAX_KEY_SIZE (32*1024)
25 /* Permit a maximum clock skew of 1h 10min. This should be enough to deal with DST confusion */
26 #define SKEW_MAX (1*USEC_PER_HOUR + 10*USEC_PER_MINUTE)
28 /* Maximum number of NSEC3 iterations we'll do. RFC5155 says 2500 shall be the maximum useful value */
29 #define NSEC3_ITERATIONS_MAX 2500
32 * The DNSSEC Chain of trust:
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
39 * Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS
42 uint16_t dnssec_keytag(DnsResourceRecord
*dnskey
, bool mask_revoke
) {
47 /* The algorithm from RFC 4034, Appendix B. */
50 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
52 f
= (uint32_t) dnskey
->dnskey
.flags
;
55 f
&= ~DNSKEY_FLAG_REVOKE
;
57 sum
= f
+ ((((uint32_t) dnskey
->dnskey
.protocol
) << 8) + (uint32_t) dnskey
->dnskey
.algorithm
);
59 p
= dnskey
->dnskey
.key
;
61 for (i
= 0; i
< dnskey
->dnskey
.key_size
; i
++)
62 sum
+= (i
& 1) == 0 ? (uint32_t) p
[i
] << 8 : (uint32_t) p
[i
];
64 sum
+= (sum
>> 16) & UINT32_C(0xFFFF);
66 return sum
& UINT32_C(0xFFFF);
69 int dnssec_canonicalize(const char *n
, char *buffer
, size_t buffer_max
) {
73 /* Converts the specified hostname into DNSSEC canonicalized
80 r
= dns_label_unescape(&n
, buffer
, buffer_max
);
86 if (buffer_max
< (size_t) r
+ 2)
89 /* The DNSSEC canonical form is not clear on what to
90 * do with dots appearing in labels, the way DNS-SD
91 * does it. Refuse it for now. */
93 if (memchr(buffer
, '.', r
))
96 ascii_strlower_n(buffer
, (size_t) r
);
106 /* Not even a single label: this is the root domain name */
108 assert(buffer_max
> 2);
120 static int rr_compare(const void *a
, const void *b
) {
121 DnsResourceRecord
**x
= (DnsResourceRecord
**) a
, **y
= (DnsResourceRecord
**) b
;
125 /* Let's order the RRs according to RFC 4034, Section 6.3 */
129 assert((*x
)->wire_format
);
132 assert((*y
)->wire_format
);
134 m
= MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(*x
), DNS_RESOURCE_RECORD_RDATA_SIZE(*y
));
136 r
= memcmp(DNS_RESOURCE_RECORD_RDATA(*x
), DNS_RESOURCE_RECORD_RDATA(*y
), m
);
140 if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x
) < DNS_RESOURCE_RECORD_RDATA_SIZE(*y
))
142 else if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x
) > DNS_RESOURCE_RECORD_RDATA_SIZE(*y
))
148 static int dnssec_rsa_verify_raw(
149 const char *hash_algorithm
,
150 const void *signature
, size_t signature_size
,
151 const void *data
, size_t data_size
,
152 const void *exponent
, size_t exponent_size
,
153 const void *modulus
, size_t modulus_size
) {
155 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
156 gcry_mpi_t n
= NULL
, e
= NULL
, s
= NULL
;
160 assert(hash_algorithm
);
162 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature
, signature_size
, NULL
);
168 ge
= gcry_mpi_scan(&e
, GCRYMPI_FMT_USG
, exponent
, exponent_size
, NULL
);
174 ge
= gcry_mpi_scan(&n
, GCRYMPI_FMT_USG
, modulus
, modulus_size
, NULL
);
180 ge
= gcry_sexp_build(&signature_sexp
,
182 "(sig-val (rsa (s %m)))",
190 ge
= gcry_sexp_build(&data_sexp
,
192 "(data (flags pkcs1) (hash %s %b))",
201 ge
= gcry_sexp_build(&public_key_sexp
,
203 "(public-key (rsa (n %m) (e %m)))",
211 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
212 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
215 log_debug("RSA signature check failed: %s", gpg_strerror(ge
));
229 gcry_sexp_release(public_key_sexp
);
231 gcry_sexp_release(signature_sexp
);
233 gcry_sexp_release(data_sexp
);
238 static int dnssec_rsa_verify(
239 const char *hash_algorithm
,
240 const void *hash
, size_t hash_size
,
241 DnsResourceRecord
*rrsig
,
242 DnsResourceRecord
*dnskey
) {
244 size_t exponent_size
, modulus_size
;
245 void *exponent
, *modulus
;
247 assert(hash_algorithm
);
249 assert(hash_size
> 0);
253 if (*(uint8_t*) dnskey
->dnskey
.key
== 0) {
254 /* exponent is > 255 bytes long */
256 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 3;
258 ((size_t) (((uint8_t*) dnskey
->dnskey
.key
)[1]) << 8) |
259 ((size_t) ((uint8_t*) dnskey
->dnskey
.key
)[2]);
261 if (exponent_size
< 256)
264 if (3 + exponent_size
>= dnskey
->dnskey
.key_size
)
267 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 3 + exponent_size
;
268 modulus_size
= dnskey
->dnskey
.key_size
- 3 - exponent_size
;
271 /* exponent is <= 255 bytes long */
273 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 1;
274 exponent_size
= (size_t) ((uint8_t*) dnskey
->dnskey
.key
)[0];
276 if (exponent_size
<= 0)
279 if (1 + exponent_size
>= dnskey
->dnskey
.key_size
)
282 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 1 + exponent_size
;
283 modulus_size
= dnskey
->dnskey
.key_size
- 1 - exponent_size
;
286 return dnssec_rsa_verify_raw(
288 rrsig
->rrsig
.signature
, rrsig
->rrsig
.signature_size
,
290 exponent
, exponent_size
,
291 modulus
, modulus_size
);
294 static int dnssec_ecdsa_verify_raw(
295 const char *hash_algorithm
,
297 const void *signature_r
, size_t signature_r_size
,
298 const void *signature_s
, size_t signature_s_size
,
299 const void *data
, size_t data_size
,
300 const void *key
, size_t key_size
) {
302 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
303 gcry_mpi_t q
= NULL
, r
= NULL
, s
= NULL
;
307 assert(hash_algorithm
);
309 ge
= gcry_mpi_scan(&r
, GCRYMPI_FMT_USG
, signature_r
, signature_r_size
, NULL
);
315 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature_s
, signature_s_size
, NULL
);
321 ge
= gcry_mpi_scan(&q
, GCRYMPI_FMT_USG
, key
, key_size
, NULL
);
327 ge
= gcry_sexp_build(&signature_sexp
,
329 "(sig-val (ecdsa (r %m) (s %m)))",
337 ge
= gcry_sexp_build(&data_sexp
,
339 "(data (flags rfc6979) (hash %s %b))",
348 ge
= gcry_sexp_build(&public_key_sexp
,
350 "(public-key (ecc (curve %s) (q %m)))",
358 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
359 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
362 log_debug("ECDSA signature check failed: %s", gpg_strerror(ge
));
375 gcry_sexp_release(public_key_sexp
);
377 gcry_sexp_release(signature_sexp
);
379 gcry_sexp_release(data_sexp
);
384 static int dnssec_ecdsa_verify(
385 const char *hash_algorithm
,
387 const void *hash
, size_t hash_size
,
388 DnsResourceRecord
*rrsig
,
389 DnsResourceRecord
*dnskey
) {
400 if (algorithm
== DNSSEC_ALGORITHM_ECDSAP256SHA256
) {
402 curve
= "NIST P-256";
403 } else if (algorithm
== DNSSEC_ALGORITHM_ECDSAP384SHA384
) {
405 curve
= "NIST P-384";
409 if (dnskey
->dnskey
.key_size
!= key_size
* 2)
412 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
415 q
= alloca(key_size
*2 + 1);
416 q
[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
417 memcpy(q
+1, dnskey
->dnskey
.key
, key_size
*2);
419 return dnssec_ecdsa_verify_raw(
422 rrsig
->rrsig
.signature
, key_size
,
423 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
428 #if GCRYPT_VERSION_NUMBER >= 0x010600
429 static int dnssec_eddsa_verify_raw(
431 const void *signature_r
, size_t signature_r_size
,
432 const void *signature_s
, size_t signature_s_size
,
433 const void *data
, size_t data_size
,
434 const void *key
, size_t key_size
) {
436 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
440 ge
= gcry_sexp_build(&signature_sexp
,
442 "(sig-val (eddsa (r %b) (s %b)))",
443 (int) signature_r_size
,
445 (int) signature_s_size
,
452 ge
= gcry_sexp_build(&data_sexp
,
454 "(data (flags eddsa) (hash-algo sha512) (value %b))",
462 ge
= gcry_sexp_build(&public_key_sexp
,
464 "(public-key (ecc (curve %s) (flags eddsa) (q %b)))",
473 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
474 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
477 log_debug("EdDSA signature check failed: %s", gpg_strerror(ge
));
483 gcry_sexp_release(public_key_sexp
);
485 gcry_sexp_release(signature_sexp
);
487 gcry_sexp_release(data_sexp
);
492 static int dnssec_eddsa_verify(
494 const void *data
, size_t data_size
,
495 DnsResourceRecord
*rrsig
,
496 DnsResourceRecord
*dnskey
) {
500 if (algorithm
== DNSSEC_ALGORITHM_ED25519
) {
506 if (dnskey
->dnskey
.key_size
!= key_size
)
509 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
512 return dnssec_eddsa_verify_raw(
514 rrsig
->rrsig
.signature
, key_size
,
515 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
517 dnskey
->dnskey
.key
, key_size
);
521 static void md_add_uint8(gcry_md_hd_t md
, uint8_t v
) {
522 gcry_md_write(md
, &v
, sizeof(v
));
525 static void md_add_uint16(gcry_md_hd_t md
, uint16_t v
) {
527 gcry_md_write(md
, &v
, sizeof(v
));
530 static void fwrite_uint8(FILE *fp
, uint8_t v
) {
531 fwrite(&v
, sizeof(v
), 1, fp
);
534 static void fwrite_uint16(FILE *fp
, uint16_t v
) {
536 fwrite(&v
, sizeof(v
), 1, fp
);
539 static void fwrite_uint32(FILE *fp
, uint32_t v
) {
541 fwrite(&v
, sizeof(v
), 1, fp
);
544 static int dnssec_rrsig_prepare(DnsResourceRecord
*rrsig
) {
545 int n_key_labels
, n_signer_labels
;
549 /* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source and
550 * .n_skip_labels_signer fields so that we can use them later on. */
553 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
555 /* Check if this RRSIG RR is already prepared */
556 if (rrsig
->n_skip_labels_source
!= (unsigned) -1)
559 if (rrsig
->rrsig
.inception
> rrsig
->rrsig
.expiration
)
562 name
= dns_resource_key_name(rrsig
->key
);
564 n_key_labels
= dns_name_count_labels(name
);
565 if (n_key_labels
< 0)
567 if (rrsig
->rrsig
.labels
> n_key_labels
)
570 n_signer_labels
= dns_name_count_labels(rrsig
->rrsig
.signer
);
571 if (n_signer_labels
< 0)
572 return n_signer_labels
;
573 if (n_signer_labels
> rrsig
->rrsig
.labels
)
576 r
= dns_name_skip(name
, n_key_labels
- n_signer_labels
, &name
);
582 /* Check if the signer is really a suffix of us */
583 r
= dns_name_equal(name
, rrsig
->rrsig
.signer
);
589 rrsig
->n_skip_labels_source
= n_key_labels
- rrsig
->rrsig
.labels
;
590 rrsig
->n_skip_labels_signer
= n_key_labels
- n_signer_labels
;
595 static int dnssec_rrsig_expired(DnsResourceRecord
*rrsig
, usec_t realtime
) {
596 usec_t expiration
, inception
, skew
;
599 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
601 if (realtime
== USEC_INFINITY
)
602 realtime
= now(CLOCK_REALTIME
);
604 expiration
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
605 inception
= rrsig
->rrsig
.inception
* USEC_PER_SEC
;
607 /* Consider inverted validity intervals as expired */
608 if (inception
> expiration
)
611 /* Permit a certain amount of clock skew of 10% of the valid
612 * time range. This takes inspiration from unbound's
614 skew
= (expiration
- inception
) / 10;
618 if (inception
< skew
)
623 if (expiration
+ skew
< expiration
)
624 expiration
= USEC_INFINITY
;
628 return realtime
< inception
|| realtime
> expiration
;
631 static int algorithm_to_gcrypt_md(uint8_t algorithm
) {
633 /* Translates a DNSSEC signature algorithm into a gcrypt
636 * Note that we implement all algorithms listed as "Must
637 * implement" and "Recommended to Implement" in RFC6944. We
638 * don't implement any algorithms that are listed as
639 * "Optional" or "Must Not Implement". Specifically, we do not
640 * implement RSAMD5, DSASHA1, DH, DSA-NSEC3-SHA1, and
645 case DNSSEC_ALGORITHM_RSASHA1
:
646 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
649 case DNSSEC_ALGORITHM_RSASHA256
:
650 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
651 return GCRY_MD_SHA256
;
653 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
654 return GCRY_MD_SHA384
;
656 case DNSSEC_ALGORITHM_RSASHA512
:
657 return GCRY_MD_SHA512
;
664 static void dnssec_fix_rrset_ttl(
665 DnsResourceRecord
*list
[],
667 DnsResourceRecord
*rrsig
,
676 for (k
= 0; k
< n
; k
++) {
677 DnsResourceRecord
*rr
= list
[k
];
679 /* Pick the TTL as the minimum of the RR's TTL, the
680 * RR's original TTL according to the RRSIG and the
681 * RRSIG's own TTL, see RFC 4035, Section 5.3.3 */
682 rr
->ttl
= MIN3(rr
->ttl
, rrsig
->rrsig
.original_ttl
, rrsig
->ttl
);
683 rr
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
685 /* Copy over information about the signer and wildcard source of synthesis */
686 rr
->n_skip_labels_source
= rrsig
->n_skip_labels_source
;
687 rr
->n_skip_labels_signer
= rrsig
->n_skip_labels_signer
;
690 rrsig
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
693 int dnssec_verify_rrset(
695 const DnsResourceKey
*key
,
696 DnsResourceRecord
*rrsig
,
697 DnsResourceRecord
*dnskey
,
699 DnssecResult
*result
) {
701 uint8_t wire_format_name
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
702 DnsResourceRecord
**list
, *rr
;
703 const char *source
, *name
;
704 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
708 _cleanup_free_
char *sig_data
= NULL
;
709 _cleanup_fclose_
FILE *f
= NULL
;
718 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
719 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
721 /* Verifies that the RRSet matches the specified "key" in "a",
722 * using the signature "rrsig" and the key "dnskey". It's
723 * assumed that RRSIG and DNSKEY match. */
725 r
= dnssec_rrsig_prepare(rrsig
);
727 *result
= DNSSEC_INVALID
;
733 r
= dnssec_rrsig_expired(rrsig
, realtime
);
737 *result
= DNSSEC_SIGNATURE_EXPIRED
;
741 name
= dns_resource_key_name(key
);
743 /* Some keys may only appear signed in the zone apex, and are invalid anywhere else. (SOA, NS...) */
744 if (dns_type_apex_only(rrsig
->rrsig
.type_covered
)) {
745 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
749 *result
= DNSSEC_INVALID
;
754 /* OTOH DS RRs may not appear in the zone apex, but are valid everywhere else. */
755 if (rrsig
->rrsig
.type_covered
== DNS_TYPE_DS
) {
756 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
760 *result
= DNSSEC_INVALID
;
765 /* Determine the "Source of Synthesis" and whether this is a wildcard RRSIG */
766 r
= dns_name_suffix(name
, rrsig
->rrsig
.labels
, &source
);
769 if (r
> 0 && !dns_type_may_wildcard(rrsig
->rrsig
.type_covered
)) {
770 /* We refuse to validate NSEC3 or SOA RRs that are synthesized from wildcards */
771 *result
= DNSSEC_INVALID
;
775 /* If we stripped a single label, then let's see if that maybe was "*". If so, we are not really
776 * synthesized from a wildcard, we are the wildcard itself. Treat that like a normal name. */
777 r
= dns_name_startswith(name
, "*");
787 /* Collect all relevant RRs in a single array, so that we can look at the RRset */
788 list
= newa(DnsResourceRecord
*, dns_answer_size(a
));
790 DNS_ANSWER_FOREACH(rr
, a
) {
791 r
= dns_resource_key_equal(key
, rr
->key
);
797 /* We need the wire format for ordering, and digest calculation */
798 r
= dns_resource_record_to_wire_format(rr
, true);
804 if (n
> VERIFY_RRS_MAX
)
811 /* Bring the RRs into canonical order */
812 qsort_safe(list
, n
, sizeof(DnsResourceRecord
*), rr_compare
);
814 f
= open_memstream(&sig_data
, &sig_size
);
817 __fsetlocking(f
, FSETLOCKING_BYCALLER
);
819 fwrite_uint16(f
, rrsig
->rrsig
.type_covered
);
820 fwrite_uint8(f
, rrsig
->rrsig
.algorithm
);
821 fwrite_uint8(f
, rrsig
->rrsig
.labels
);
822 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
823 fwrite_uint32(f
, rrsig
->rrsig
.expiration
);
824 fwrite_uint32(f
, rrsig
->rrsig
.inception
);
825 fwrite_uint16(f
, rrsig
->rrsig
.key_tag
);
827 r
= dns_name_to_wire_format(rrsig
->rrsig
.signer
, wire_format_name
, sizeof(wire_format_name
), true);
830 fwrite(wire_format_name
, 1, r
, f
);
832 /* Convert the source of synthesis into wire format */
833 r
= dns_name_to_wire_format(source
, wire_format_name
, sizeof(wire_format_name
), true);
837 for (k
= 0; k
< n
; k
++) {
842 /* Hash the source of synthesis. If this is a wildcard, then prefix it with the *. label */
844 fwrite((uint8_t[]) { 1, '*'}, sizeof(uint8_t), 2, f
);
845 fwrite(wire_format_name
, 1, r
, f
);
847 fwrite_uint16(f
, rr
->key
->type
);
848 fwrite_uint16(f
, rr
->key
->class);
849 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
851 l
= DNS_RESOURCE_RECORD_RDATA_SIZE(rr
);
854 fwrite_uint16(f
, (uint16_t) l
);
855 fwrite(DNS_RESOURCE_RECORD_RDATA(rr
), 1, l
, f
);
858 r
= fflush_and_check(f
);
862 initialize_libgcrypt(false);
864 switch (rrsig
->rrsig
.algorithm
) {
865 #if GCRYPT_VERSION_NUMBER >= 0x010600
866 case DNSSEC_ALGORITHM_ED25519
:
869 case DNSSEC_ALGORITHM_ED25519
:
871 case DNSSEC_ALGORITHM_ED448
:
872 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
875 /* OK, the RRs are now in canonical order. Let's calculate the digest */
876 md_algorithm
= algorithm_to_gcrypt_md(rrsig
->rrsig
.algorithm
);
877 if (md_algorithm
== -EOPNOTSUPP
) {
878 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
881 if (md_algorithm
< 0)
884 gcry_md_open(&md
, md_algorithm
, 0);
888 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
889 assert(hash_size
> 0);
891 gcry_md_write(md
, sig_data
, sig_size
);
893 hash
= gcry_md_read(md
, 0);
898 switch (rrsig
->rrsig
.algorithm
) {
900 case DNSSEC_ALGORITHM_RSASHA1
:
901 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
902 case DNSSEC_ALGORITHM_RSASHA256
:
903 case DNSSEC_ALGORITHM_RSASHA512
:
904 r
= dnssec_rsa_verify(
905 gcry_md_algo_name(md_algorithm
),
911 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
912 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
913 r
= dnssec_ecdsa_verify(
914 gcry_md_algo_name(md_algorithm
),
915 rrsig
->rrsig
.algorithm
,
920 #if GCRYPT_VERSION_NUMBER >= 0x010600
921 case DNSSEC_ALGORITHM_ED25519
:
922 r
= dnssec_eddsa_verify(
923 rrsig
->rrsig
.algorithm
,
933 /* Now, fix the ttl, expiry, and remember the synthesizing source and the signer */
935 dnssec_fix_rrset_ttl(list
, n
, rrsig
, realtime
);
938 *result
= DNSSEC_INVALID
;
940 *result
= DNSSEC_VALIDATED_WILDCARD
;
942 *result
= DNSSEC_VALIDATED
;
947 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
952 /* Checks if the specified DNSKEY RR matches the key used for
953 * the signature in the specified RRSIG RR */
955 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
958 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
960 if (dnskey
->key
->class != rrsig
->key
->class)
962 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
964 if (!revoked_ok
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
966 if (dnskey
->dnskey
.protocol
!= 3)
968 if (dnskey
->dnskey
.algorithm
!= rrsig
->rrsig
.algorithm
)
971 if (dnssec_keytag(dnskey
, false) != rrsig
->rrsig
.key_tag
)
974 return dns_name_equal(dns_resource_key_name(dnskey
->key
), rrsig
->rrsig
.signer
);
977 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
981 /* Checks if the specified RRSIG RR protects the RRSet of the specified RR key. */
983 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
985 if (rrsig
->key
->class != key
->class)
987 if (rrsig
->rrsig
.type_covered
!= key
->type
)
990 return dns_name_equal(dns_resource_key_name(rrsig
->key
), dns_resource_key_name(key
));
993 int dnssec_verify_rrset_search(
995 const DnsResourceKey
*key
,
996 DnsAnswer
*validated_dnskeys
,
998 DnssecResult
*result
,
999 DnsResourceRecord
**ret_rrsig
) {
1001 bool found_rrsig
= false, found_invalid
= false, found_expired_rrsig
= false, found_unsupported_algorithm
= false;
1002 DnsResourceRecord
*rrsig
;
1008 /* Verifies all RRs from "a" that match the key "key" against DNSKEYs in "validated_dnskeys" */
1010 if (!a
|| a
->n_rrs
<= 0)
1013 /* Iterate through each RRSIG RR. */
1014 DNS_ANSWER_FOREACH(rrsig
, a
) {
1015 DnsResourceRecord
*dnskey
;
1016 DnsAnswerFlags flags
;
1018 /* Is this an RRSIG RR that applies to RRs matching our key? */
1019 r
= dnssec_key_match_rrsig(key
, rrsig
);
1027 /* Look for a matching key */
1028 DNS_ANSWER_FOREACH_FLAGS(dnskey
, flags
, validated_dnskeys
) {
1029 DnssecResult one_result
;
1031 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1034 /* Is this a DNSKEY RR that matches they key of our RRSIG? */
1035 r
= dnssec_rrsig_match_dnskey(rrsig
, dnskey
, false);
1041 /* Take the time here, if it isn't set yet, so
1042 * that we do all validations with the same
1044 if (realtime
== USEC_INFINITY
)
1045 realtime
= now(CLOCK_REALTIME
);
1047 /* Yay, we found a matching RRSIG with a matching
1048 * DNSKEY, awesome. Now let's verify all entries of
1049 * the RRSet against the RRSIG and DNSKEY
1052 r
= dnssec_verify_rrset(a
, key
, rrsig
, dnskey
, realtime
, &one_result
);
1056 switch (one_result
) {
1058 case DNSSEC_VALIDATED
:
1059 case DNSSEC_VALIDATED_WILDCARD
:
1060 /* Yay, the RR has been validated,
1061 * return immediately, but fix up the expiry */
1065 *result
= one_result
;
1068 case DNSSEC_INVALID
:
1069 /* If the signature is invalid, let's try another
1070 key and/or signature. After all they
1071 key_tags and stuff are not unique, and
1072 might be shared by multiple keys. */
1073 found_invalid
= true;
1076 case DNSSEC_UNSUPPORTED_ALGORITHM
:
1077 /* If the key algorithm is
1078 unsupported, try another
1079 RRSIG/DNSKEY pair, but remember we
1080 encountered this, so that we can
1081 return a proper error when we
1082 encounter nothing better. */
1083 found_unsupported_algorithm
= true;
1086 case DNSSEC_SIGNATURE_EXPIRED
:
1087 /* If the signature is expired, try
1088 another one, but remember it, so
1089 that we can return this */
1090 found_expired_rrsig
= true;
1094 assert_not_reached("Unexpected DNSSEC validation result");
1099 if (found_expired_rrsig
)
1100 *result
= DNSSEC_SIGNATURE_EXPIRED
;
1101 else if (found_unsupported_algorithm
)
1102 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
1103 else if (found_invalid
)
1104 *result
= DNSSEC_INVALID
;
1105 else if (found_rrsig
)
1106 *result
= DNSSEC_MISSING_KEY
;
1108 *result
= DNSSEC_NO_SIGNATURE
;
1116 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
1117 DnsResourceRecord
*rr
;
1120 /* Checks whether there's at least one RRSIG in 'a' that proctects RRs of the specified key */
1122 DNS_ANSWER_FOREACH(rr
, a
) {
1123 r
= dnssec_key_match_rrsig(key
, rr
);
1133 static int digest_to_gcrypt_md(uint8_t algorithm
) {
1135 /* Translates a DNSSEC digest algorithm into a gcrypt digest identifier */
1137 switch (algorithm
) {
1139 case DNSSEC_DIGEST_SHA1
:
1140 return GCRY_MD_SHA1
;
1142 case DNSSEC_DIGEST_SHA256
:
1143 return GCRY_MD_SHA256
;
1145 case DNSSEC_DIGEST_SHA384
:
1146 return GCRY_MD_SHA384
;
1153 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
1154 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1155 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
1157 int md_algorithm
, r
;
1163 /* Implements DNSKEY verification by a DS, according to RFC 4035, section 5.2 */
1165 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1167 if (ds
->key
->type
!= DNS_TYPE_DS
)
1169 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
1170 return -EKEYREJECTED
;
1171 if (!mask_revoke
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
1172 return -EKEYREJECTED
;
1173 if (dnskey
->dnskey
.protocol
!= 3)
1174 return -EKEYREJECTED
;
1176 if (dnskey
->dnskey
.algorithm
!= ds
->ds
.algorithm
)
1178 if (dnssec_keytag(dnskey
, mask_revoke
) != ds
->ds
.key_tag
)
1181 initialize_libgcrypt(false);
1183 md_algorithm
= digest_to_gcrypt_md(ds
->ds
.digest_type
);
1184 if (md_algorithm
< 0)
1185 return md_algorithm
;
1187 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
1188 assert(hash_size
> 0);
1190 if (ds
->ds
.digest_size
!= hash_size
)
1193 r
= dns_name_to_wire_format(dns_resource_key_name(dnskey
->key
), wire_format
, sizeof(wire_format
), true);
1197 gcry_md_open(&md
, md_algorithm
, 0);
1201 gcry_md_write(md
, wire_format
, r
);
1203 md_add_uint16(md
, dnskey
->dnskey
.flags
& ~DNSKEY_FLAG_REVOKE
);
1205 md_add_uint16(md
, dnskey
->dnskey
.flags
);
1206 md_add_uint8(md
, dnskey
->dnskey
.protocol
);
1207 md_add_uint8(md
, dnskey
->dnskey
.algorithm
);
1208 gcry_md_write(md
, dnskey
->dnskey
.key
, dnskey
->dnskey
.key_size
);
1210 result
= gcry_md_read(md
, 0);
1214 return memcmp(result
, ds
->ds
.digest
, ds
->ds
.digest_size
) == 0;
1217 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
1218 DnsResourceRecord
*ds
;
1219 DnsAnswerFlags flags
;
1224 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1227 DNS_ANSWER_FOREACH_FLAGS(ds
, flags
, validated_ds
) {
1229 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1232 if (ds
->key
->type
!= DNS_TYPE_DS
)
1234 if (ds
->key
->class != dnskey
->key
->class)
1237 r
= dns_name_equal(dns_resource_key_name(dnskey
->key
), dns_resource_key_name(ds
->key
));
1243 r
= dnssec_verify_dnskey_by_ds(dnskey
, ds
, false);
1244 if (IN_SET(r
, -EKEYREJECTED
, -EOPNOTSUPP
))
1245 return 0; /* The DNSKEY is revoked or otherwise invalid, or we don't support the digest algorithm */
1255 static int nsec3_hash_to_gcrypt_md(uint8_t algorithm
) {
1257 /* Translates a DNSSEC NSEC3 hash algorithm into a gcrypt digest identifier */
1259 switch (algorithm
) {
1261 case NSEC3_ALGORITHM_SHA1
:
1262 return GCRY_MD_SHA1
;
1269 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
1270 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1271 gcry_md_hd_t md
= NULL
;
1282 if (nsec3
->key
->type
!= DNS_TYPE_NSEC3
)
1285 if (nsec3
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
) {
1286 log_debug("Ignoring NSEC3 RR %s with excessive number of iterations.", dns_resource_record_to_string(nsec3
));
1290 algorithm
= nsec3_hash_to_gcrypt_md(nsec3
->nsec3
.algorithm
);
1294 initialize_libgcrypt(false);
1296 hash_size
= gcry_md_get_algo_dlen(algorithm
);
1297 assert(hash_size
> 0);
1299 if (nsec3
->nsec3
.next_hashed_name_size
!= hash_size
)
1302 r
= dns_name_to_wire_format(name
, wire_format
, sizeof(wire_format
), true);
1306 gcry_md_open(&md
, algorithm
, 0);
1310 gcry_md_write(md
, wire_format
, r
);
1311 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1313 result
= gcry_md_read(md
, 0);
1319 for (k
= 0; k
< nsec3
->nsec3
.iterations
; k
++) {
1320 uint8_t tmp
[hash_size
];
1321 memcpy(tmp
, result
, hash_size
);
1324 gcry_md_write(md
, tmp
, hash_size
);
1325 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1327 result
= gcry_md_read(md
, 0);
1334 memcpy(ret
, result
, hash_size
);
1335 r
= (int) hash_size
;
1342 static int nsec3_is_good(DnsResourceRecord
*rr
, DnsResourceRecord
*nsec3
) {
1348 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
1351 /* RFC 5155, Section 8.2 says we MUST ignore NSEC3 RRs with flags != 0 or 1 */
1352 if (!IN_SET(rr
->nsec3
.flags
, 0, 1))
1355 /* Ignore NSEC3 RRs whose algorithm we don't know */
1356 if (nsec3_hash_to_gcrypt_md(rr
->nsec3
.algorithm
) < 0)
1358 /* Ignore NSEC3 RRs with an excessive number of required iterations */
1359 if (rr
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1362 /* Ignore NSEC3 RRs generated from wildcards. If these NSEC3 RRs weren't correctly signed we can't make this
1363 * check (since rr->n_skip_labels_source is -1), but that's OK, as we won't trust them anyway in that case. */
1364 if (!IN_SET(rr
->n_skip_labels_source
, 0, (unsigned) -1))
1366 /* Ignore NSEC3 RRs that are located anywhere else than one label below the zone */
1367 if (!IN_SET(rr
->n_skip_labels_signer
, 1, (unsigned) -1))
1373 /* If a second NSEC3 RR is specified, also check if they are from the same zone. */
1375 if (nsec3
== rr
) /* Shortcut */
1378 if (rr
->key
->class != nsec3
->key
->class)
1380 if (rr
->nsec3
.algorithm
!= nsec3
->nsec3
.algorithm
)
1382 if (rr
->nsec3
.iterations
!= nsec3
->nsec3
.iterations
)
1384 if (rr
->nsec3
.salt_size
!= nsec3
->nsec3
.salt_size
)
1386 if (memcmp(rr
->nsec3
.salt
, nsec3
->nsec3
.salt
, rr
->nsec3
.salt_size
) != 0)
1389 a
= dns_resource_key_name(rr
->key
);
1390 r
= dns_name_parent(&a
); /* strip off hash */
1396 b
= dns_resource_key_name(nsec3
->key
);
1397 r
= dns_name_parent(&b
); /* strip off hash */
1403 /* Make sure both have the same parent */
1404 return dns_name_equal(a
, b
);
1407 static int nsec3_hashed_domain_format(const uint8_t *hashed
, size_t hashed_size
, const char *zone
, char **ret
) {
1408 _cleanup_free_
char *l
= NULL
;
1412 assert(hashed_size
> 0);
1416 l
= base32hexmem(hashed
, hashed_size
, false);
1420 j
= strjoin(l
, ".", zone
);
1425 return (int) hashed_size
;
1428 static int nsec3_hashed_domain_make(DnsResourceRecord
*nsec3
, const char *domain
, const char *zone
, char **ret
) {
1429 uint8_t hashed
[DNSSEC_HASH_SIZE_MAX
];
1437 hashed_size
= dnssec_nsec3_hash(nsec3
, domain
, hashed
);
1438 if (hashed_size
< 0)
1441 return nsec3_hashed_domain_format(hashed
, (size_t) hashed_size
, zone
, ret
);
1444 /* See RFC 5155, Section 8
1445 * First try to find a NSEC3 record that matches our query precisely, if that fails, find the closest
1446 * enclosure. Secondly, find a proof that there is no closer enclosure and either a proof that there
1447 * is no wildcard domain as a direct descendant of the closest enclosure, or find an NSEC3 record that
1448 * matches the wildcard domain.
1450 * Based on this we can prove either the existence of the record in @key, or NXDOMAIN or NODATA, or
1451 * that there is no proof either way. The latter is the case if a the proof of non-existence of a given
1452 * name uses an NSEC3 record with the opt-out bit set. Lastly, if we are given insufficient NSEC3 records
1453 * to conclude anything we indicate this by returning NO_RR. */
1454 static int dnssec_test_nsec3(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1455 _cleanup_free_
char *next_closer_domain
= NULL
, *wildcard_domain
= NULL
;
1456 const char *zone
, *p
, *pp
= NULL
, *wildcard
;
1457 DnsResourceRecord
*rr
, *enclosure_rr
, *zone_rr
, *wildcard_rr
= NULL
;
1458 DnsAnswerFlags flags
;
1460 bool a
, no_closer
= false, no_wildcard
= false, optout
= false;
1465 /* First step, find the zone name and the NSEC3 parameters of the zone.
1466 * it is sufficient to look for the longest common suffix we find with
1467 * any NSEC3 RR in the response. Any NSEC3 record will do as all NSEC3
1468 * records from a given zone in a response must use the same
1470 zone
= dns_resource_key_name(key
);
1472 DNS_ANSWER_FOREACH_FLAGS(zone_rr
, flags
, answer
) {
1473 r
= nsec3_is_good(zone_rr
, NULL
);
1479 r
= dns_name_equal_skip(dns_resource_key_name(zone_rr
->key
), 1, zone
);
1486 /* Strip one label from the front */
1487 r
= dns_name_parent(&zone
);
1494 *result
= DNSSEC_NSEC_NO_RR
;
1498 /* Second step, find the closest encloser NSEC3 RR in 'answer' that matches 'key' */
1499 p
= dns_resource_key_name(key
);
1501 _cleanup_free_
char *hashed_domain
= NULL
;
1503 hashed_size
= nsec3_hashed_domain_make(zone_rr
, p
, zone
, &hashed_domain
);
1504 if (hashed_size
== -EOPNOTSUPP
) {
1505 *result
= DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
;
1508 if (hashed_size
< 0)
1511 DNS_ANSWER_FOREACH_FLAGS(enclosure_rr
, flags
, answer
) {
1513 r
= nsec3_is_good(enclosure_rr
, zone_rr
);
1519 if (enclosure_rr
->nsec3
.next_hashed_name_size
!= (size_t) hashed_size
)
1522 r
= dns_name_equal(dns_resource_key_name(enclosure_rr
->key
), hashed_domain
);
1526 a
= flags
& DNS_ANSWER_AUTHENTICATED
;
1527 goto found_closest_encloser
;
1531 /* We didn't find the closest encloser with this name,
1532 * but let's remember this domain name, it might be
1533 * the next closer name */
1537 /* Strip one label from the front */
1538 r
= dns_name_parent(&p
);
1545 *result
= DNSSEC_NSEC_NO_RR
;
1548 found_closest_encloser
:
1549 /* We found a closest encloser in 'p'; next closer is 'pp' */
1552 /* We have an exact match! If we area looking for a DS RR, then we must insist that we got the NSEC3 RR
1553 * from the parent. Otherwise the one from the child. Do so, by checking whether SOA and NS are
1554 * appropriately set. */
1556 if (key
->type
== DNS_TYPE_DS
) {
1557 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1560 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1561 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1565 /* No next closer NSEC3 RR. That means there's a direct NSEC3 RR for our key. */
1566 if (bitmap_isset(enclosure_rr
->nsec3
.types
, key
->type
))
1567 *result
= DNSSEC_NSEC_FOUND
;
1568 else if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1569 *result
= DNSSEC_NSEC_CNAME
;
1571 *result
= DNSSEC_NSEC_NODATA
;
1576 *ttl
= enclosure_rr
->ttl
;
1581 /* Ensure this is not a DNAME domain, see RFC5155, section 8.3. */
1582 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_DNAME
))
1585 /* Ensure that this data is from the delegated domain
1586 * (i.e. originates from the "lower" DNS server), and isn't
1587 * just glue records (i.e. doesn't originate from the "upper"
1589 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1590 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1593 /* Prove that there is no next closer and whether or not there is a wildcard domain. */
1595 wildcard
= strjoina("*.", p
);
1596 r
= nsec3_hashed_domain_make(enclosure_rr
, wildcard
, zone
, &wildcard_domain
);
1599 if (r
!= hashed_size
)
1602 r
= nsec3_hashed_domain_make(enclosure_rr
, pp
, zone
, &next_closer_domain
);
1605 if (r
!= hashed_size
)
1608 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1609 _cleanup_free_
char *next_hashed_domain
= NULL
;
1611 r
= nsec3_is_good(rr
, zone_rr
);
1617 r
= nsec3_hashed_domain_format(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, zone
, &next_hashed_domain
);
1621 r
= dns_name_between(dns_resource_key_name(rr
->key
), next_closer_domain
, next_hashed_domain
);
1625 if (rr
->nsec3
.flags
& 1)
1628 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1633 r
= dns_name_equal(dns_resource_key_name(rr
->key
), wildcard_domain
);
1637 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1642 r
= dns_name_between(dns_resource_key_name(rr
->key
), wildcard_domain
, next_hashed_domain
);
1646 if (rr
->nsec3
.flags
& 1)
1647 /* This only makes sense if we have a wildcard delegation, which is
1648 * very unlikely, see RFC 4592, Section 4.2, but we cannot rely on
1649 * this not happening, so hence cannot simply conclude NXDOMAIN as
1653 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1659 if (wildcard_rr
&& no_wildcard
)
1663 *result
= DNSSEC_NSEC_NO_RR
;
1668 /* A wildcard exists that matches our query. */
1670 /* This is not specified in any RFC to the best of my knowledge, but
1671 * if the next closer enclosure is covered by an opt-out NSEC3 RR
1672 * it means that we cannot prove that the source of synthesis is
1673 * correct, as there may be a closer match. */
1674 *result
= DNSSEC_NSEC_OPTOUT
;
1675 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, key
->type
))
1676 *result
= DNSSEC_NSEC_FOUND
;
1677 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1678 *result
= DNSSEC_NSEC_CNAME
;
1680 *result
= DNSSEC_NSEC_NODATA
;
1683 /* The RFC only specifies that we have to care for optout for NODATA for
1684 * DS records. However, children of an insecure opt-out delegation should
1685 * also be considered opt-out, rather than verified NXDOMAIN.
1686 * Note that we do not require a proof of wildcard non-existence if the
1687 * next closer domain is covered by an opt-out, as that would not provide
1688 * any additional information. */
1689 *result
= DNSSEC_NSEC_OPTOUT
;
1690 else if (no_wildcard
)
1691 *result
= DNSSEC_NSEC_NXDOMAIN
;
1693 *result
= DNSSEC_NSEC_NO_RR
;
1703 *ttl
= enclosure_rr
->ttl
;
1708 static int dnssec_nsec_wildcard_equal(DnsResourceRecord
*rr
, const char *name
) {
1709 char label
[DNS_LABEL_MAX
];
1714 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1716 /* Checks whether the specified RR has a name beginning in "*.", and if the rest is a suffix of our name */
1718 if (rr
->n_skip_labels_source
!= 1)
1721 n
= dns_resource_key_name(rr
->key
);
1722 r
= dns_label_unescape(&n
, label
, sizeof(label
));
1725 if (r
!= 1 || label
[0] != '*')
1728 return dns_name_endswith(name
, n
);
1731 static int dnssec_nsec_in_path(DnsResourceRecord
*rr
, const char *name
) {
1732 const char *nn
, *common_suffix
;
1736 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1738 /* Checks whether the specified nsec RR indicates that name is an empty non-terminal (ENT)
1740 * A couple of examples:
1742 * NSEC bar → waldo.foo.bar: indicates that foo.bar exists and is an ENT
1743 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that xoo.bar and zzz.xoo.bar exist and are ENTs
1744 * NSEC yyy.zzz.xoo.bar → bar: indicates pretty much nothing about ENTs
1747 /* First, determine parent of next domain. */
1748 nn
= rr
->nsec
.next_domain_name
;
1749 r
= dns_name_parent(&nn
);
1753 /* If the name we just determined is not equal or child of the name we are interested in, then we can't say
1754 * anything at all. */
1755 r
= dns_name_endswith(nn
, name
);
1759 /* 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. */
1760 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
1764 return dns_name_endswith(name
, common_suffix
);
1767 static int dnssec_nsec_from_parent_zone(DnsResourceRecord
*rr
, const char *name
) {
1771 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1773 /* Checks whether this NSEC originates to the parent zone or the child zone. */
1775 r
= dns_name_parent(&name
);
1779 r
= dns_name_equal(name
, dns_resource_key_name(rr
->key
));
1783 /* DNAME, and NS without SOA is an indication for a delegation. */
1784 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_DNAME
))
1787 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) && !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1793 static int dnssec_nsec_covers(DnsResourceRecord
*rr
, const char *name
) {
1798 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1800 /* Checks whether the name is covered by this NSEC RR. This means, that the name is somewhere below the NSEC's
1801 * signer name, and between the NSEC's two names. */
1803 r
= dns_resource_record_signer(rr
, &signer
);
1807 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
1811 return dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
1814 static int dnssec_nsec_covers_wildcard(DnsResourceRecord
*rr
, const char *name
) {
1815 _cleanup_free_
char *wc
= NULL
;
1816 const char *common_suffix
, *signer
;
1820 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1822 /* Checks whether the "Wildcard at the Closest Encloser" is within the space covered by the specified
1823 * RR. Specifically, checks whether 'name' has the common suffix of the NSEC RR's owner and next names as
1824 * suffix, and whether the NSEC covers the name generated by that suffix prepended with an asterisk label.
1826 * NSEC bar → waldo.foo.bar: indicates that *.bar and *.foo.bar do not exist
1827 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that *.xoo.bar and *.zzz.xoo.bar do not exist (and more ...)
1828 * NSEC yyy.zzz.xoo.bar → bar: indicates that a number of wildcards don#t exist either...
1831 r
= dns_resource_record_signer(rr
, &signer
);
1835 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
1839 r
= dns_name_endswith(name
, dns_resource_key_name(rr
->key
));
1842 if (r
> 0) /* If the name we are interested in is a child of the NSEC RR, then append the asterisk to the NSEC
1844 r
= dns_name_concat("*", dns_resource_key_name(rr
->key
), &wc
);
1846 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
1850 r
= dns_name_concat("*", common_suffix
, &wc
);
1855 return dns_name_between(dns_resource_key_name(rr
->key
), wc
, rr
->nsec
.next_domain_name
);
1858 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1859 bool have_nsec3
= false, covering_rr_authenticated
= false, wildcard_rr_authenticated
= false;
1860 DnsResourceRecord
*rr
, *covering_rr
= NULL
, *wildcard_rr
= NULL
;
1861 DnsAnswerFlags flags
;
1868 /* Look for any NSEC/NSEC3 RRs that say something about the specified key. */
1870 name
= dns_resource_key_name(key
);
1872 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1874 if (rr
->key
->class != key
->class)
1877 have_nsec3
= have_nsec3
|| (rr
->key
->type
== DNS_TYPE_NSEC3
);
1879 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
1882 /* The following checks only make sense for NSEC RRs that are not expanded from a wildcard */
1883 r
= dns_resource_record_is_synthetic(rr
);
1889 /* Check if this is a direct match. If so, we have encountered a NODATA case */
1890 r
= dns_name_equal(dns_resource_key_name(rr
->key
), name
);
1894 /* If it's not a direct match, maybe it's a wild card match? */
1895 r
= dnssec_nsec_wildcard_equal(rr
, name
);
1900 if (key
->type
== DNS_TYPE_DS
) {
1901 /* If we look for a DS RR and the server sent us the NSEC RR of the child zone
1902 * we have a problem. For DS RRs we want the NSEC RR from the parent */
1903 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1906 /* For all RR types, ensure that if NS is set SOA is set too, so that we know
1907 * we got the child's NSEC. */
1908 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) &&
1909 !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1913 if (bitmap_isset(rr
->nsec
.types
, key
->type
))
1914 *result
= DNSSEC_NSEC_FOUND
;
1915 else if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_CNAME
))
1916 *result
= DNSSEC_NSEC_CNAME
;
1918 *result
= DNSSEC_NSEC_NODATA
;
1921 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1928 /* Check if the name we are looking for is an empty non-terminal within the owner or next name
1929 * of the NSEC RR. */
1930 r
= dnssec_nsec_in_path(rr
, name
);
1934 *result
= DNSSEC_NSEC_NODATA
;
1937 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1944 /* The following two "covering" checks, are not useful if the NSEC is from the parent */
1945 r
= dnssec_nsec_from_parent_zone(rr
, name
);
1951 /* Check if this NSEC RR proves the absence of an explicit RR under this name */
1952 r
= dnssec_nsec_covers(rr
, name
);
1955 if (r
> 0 && (!covering_rr
|| !covering_rr_authenticated
)) {
1957 covering_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1960 /* Check if this NSEC RR proves the absence of a wildcard RR under this name */
1961 r
= dnssec_nsec_covers_wildcard(rr
, name
);
1964 if (r
> 0 && (!wildcard_rr
|| !wildcard_rr_authenticated
)) {
1966 wildcard_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1970 if (covering_rr
&& wildcard_rr
) {
1971 /* If we could prove that neither the name itself, nor the wildcard at the closest encloser exists, we
1972 * proved the NXDOMAIN case. */
1973 *result
= DNSSEC_NSEC_NXDOMAIN
;
1976 *authenticated
= covering_rr_authenticated
&& wildcard_rr_authenticated
;
1978 *ttl
= MIN(covering_rr
->ttl
, wildcard_rr
->ttl
);
1983 /* OK, this was not sufficient. Let's see if NSEC3 can help. */
1985 return dnssec_test_nsec3(answer
, key
, result
, authenticated
, ttl
);
1987 /* No approproate NSEC RR found, report this. */
1988 *result
= DNSSEC_NSEC_NO_RR
;
1992 static int dnssec_nsec_test_enclosed(DnsAnswer
*answer
, uint16_t type
, const char *name
, const char *zone
, bool *authenticated
) {
1993 DnsResourceRecord
*rr
;
1994 DnsAnswerFlags flags
;
2000 /* Checks whether there's an NSEC/NSEC3 that proves that the specified 'name' is non-existing in the specified
2001 * 'zone'. The 'zone' must be a suffix of the 'name'. */
2003 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
2006 if (rr
->key
->type
!= type
&& type
!= DNS_TYPE_ANY
)
2009 switch (rr
->key
->type
) {
2013 /* We only care for NSEC RRs from the indicated zone */
2014 r
= dns_resource_record_is_signer(rr
, zone
);
2020 r
= dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
2027 case DNS_TYPE_NSEC3
: {
2028 _cleanup_free_
char *hashed_domain
= NULL
, *next_hashed_domain
= NULL
;
2030 /* We only care for NSEC3 RRs from the indicated zone */
2031 r
= dns_resource_record_is_signer(rr
, zone
);
2037 r
= nsec3_is_good(rr
, NULL
);
2043 /* Format the domain we are testing with the NSEC3 RR's hash function */
2044 r
= nsec3_hashed_domain_make(
2051 if ((size_t) r
!= rr
->nsec3
.next_hashed_name_size
)
2054 /* Format the NSEC3's next hashed name as proper domain name */
2055 r
= nsec3_hashed_domain_format(
2056 rr
->nsec3
.next_hashed_name
,
2057 rr
->nsec3
.next_hashed_name_size
,
2059 &next_hashed_domain
);
2063 r
= dns_name_between(dns_resource_key_name(rr
->key
), hashed_domain
, next_hashed_domain
);
2077 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2085 static int dnssec_test_positive_wildcard_nsec3(
2090 bool *authenticated
) {
2092 const char *next_closer
= NULL
;
2095 /* Run a positive NSEC3 wildcard proof. Specifically:
2097 * A proof that the "next closer" of the generating wildcard does not exist.
2099 * Note a key difference between the NSEC3 and NSEC versions of the proof. NSEC RRs don't have to exist for
2100 * empty non-transients. NSEC3 RRs however have to. This means it's sufficient to check if the next closer name
2101 * exists for the NSEC3 RR and we are done.
2103 * 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
2104 * c.d.e.f does not exist. */
2108 r
= dns_name_parent(&name
);
2114 r
= dns_name_equal(name
, source
);
2121 return dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC3
, next_closer
, zone
, authenticated
);
2124 static int dnssec_test_positive_wildcard_nsec(
2129 bool *_authenticated
) {
2131 bool authenticated
= true;
2134 /* Run a positive NSEC wildcard proof. Specifically:
2136 * A proof that there's neither a wildcard name nor a non-wildcard name that is a suffix of the name "name" and
2137 * a prefix of the synthesizing source "source" in the zone "zone".
2139 * See RFC 5155, Section 8.8 and RFC 4035, Section 5.3.4
2141 * 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
2142 * have to prove that none of the following exist:
2153 _cleanup_free_
char *wc
= NULL
;
2156 /* Check if there's an NSEC or NSEC3 RR that proves that the mame we determined is really non-existing,
2157 * i.e between the owner name and the next name of an NSEC RR. */
2158 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, name
, zone
, &a
);
2162 authenticated
= authenticated
&& a
;
2164 /* Strip one label off */
2165 r
= dns_name_parent(&name
);
2169 /* Did we reach the source of synthesis? */
2170 r
= dns_name_equal(name
, source
);
2174 /* Successful exit */
2175 *_authenticated
= authenticated
;
2179 /* Safety check, that the source of synthesis is still our suffix */
2180 r
= dns_name_endswith(name
, source
);
2186 /* Replace the label we stripped off with an asterisk */
2187 wc
= strappend("*.", name
);
2191 /* And check if the proof holds for the asterisk name, too */
2192 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, wc
, zone
, &a
);
2196 authenticated
= authenticated
&& a
;
2197 /* In the next iteration we'll check the non-asterisk-prefixed version */
2201 int dnssec_test_positive_wildcard(
2206 bool *authenticated
) {
2213 assert(authenticated
);
2215 r
= dns_answer_contains_zone_nsec3(answer
, zone
);
2219 return dnssec_test_positive_wildcard_nsec3(answer
, name
, source
, zone
, authenticated
);
2221 return dnssec_test_positive_wildcard_nsec(answer
, name
, source
, zone
, authenticated
);
2226 int dnssec_verify_rrset(
2228 const DnsResourceKey
*key
,
2229 DnsResourceRecord
*rrsig
,
2230 DnsResourceRecord
*dnskey
,
2232 DnssecResult
*result
) {
2237 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
2242 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
2247 int dnssec_verify_rrset_search(
2249 const DnsResourceKey
*key
,
2250 DnsAnswer
*validated_dnskeys
,
2252 DnssecResult
*result
,
2253 DnsResourceRecord
**ret_rrsig
) {
2258 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
2263 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
2268 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
2273 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
2278 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
2283 int dnssec_test_positive_wildcard(
2288 bool *authenticated
) {
2295 static const char* const dnssec_result_table
[_DNSSEC_RESULT_MAX
] = {
2296 [DNSSEC_VALIDATED
] = "validated",
2297 [DNSSEC_VALIDATED_WILDCARD
] = "validated-wildcard",
2298 [DNSSEC_INVALID
] = "invalid",
2299 [DNSSEC_SIGNATURE_EXPIRED
] = "signature-expired",
2300 [DNSSEC_UNSUPPORTED_ALGORITHM
] = "unsupported-algorithm",
2301 [DNSSEC_NO_SIGNATURE
] = "no-signature",
2302 [DNSSEC_MISSING_KEY
] = "missing-key",
2303 [DNSSEC_UNSIGNED
] = "unsigned",
2304 [DNSSEC_FAILED_AUXILIARY
] = "failed-auxiliary",
2305 [DNSSEC_NSEC_MISMATCH
] = "nsec-mismatch",
2306 [DNSSEC_INCOMPATIBLE_SERVER
] = "incompatible-server",
2308 DEFINE_STRING_TABLE_LOOKUP(dnssec_result
, DnssecResult
);
2310 static const char* const dnssec_verdict_table
[_DNSSEC_VERDICT_MAX
] = {
2311 [DNSSEC_SECURE
] = "secure",
2312 [DNSSEC_INSECURE
] = "insecure",
2313 [DNSSEC_BOGUS
] = "bogus",
2314 [DNSSEC_INDETERMINATE
] = "indeterminate",
2316 DEFINE_STRING_TABLE_LOOKUP(dnssec_verdict
, DnssecVerdict
);