1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "dns-domain.h"
13 #include "gcrypt-util.h"
14 #include "hexdecoct.h"
15 #include "memory-util.h"
16 #include "resolved-dns-dnssec.h"
17 #include "resolved-dns-packet.h"
18 #include "string-table.h"
20 #define VERIFY_RRS_MAX 256
21 #define MAX_KEY_SIZE (32*1024)
23 /* Permit a maximum clock skew of 1h 10min. This should be enough to deal with DST confusion */
24 #define SKEW_MAX (1*USEC_PER_HOUR + 10*USEC_PER_MINUTE)
26 /* Maximum number of NSEC3 iterations we'll do. RFC5155 says 2500 shall be the maximum useful value */
27 #define NSEC3_ITERATIONS_MAX 2500
30 * The DNSSEC Chain of trust:
32 * Normal RRs are protected via RRSIG RRs in combination with DNSKEY RRs, all in the same zone
33 * DNSKEY RRs are either protected like normal RRs, or via a DS from a zone "higher" up the tree
34 * DS RRs are protected like normal RRs
37 * Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS
40 uint16_t dnssec_keytag(DnsResourceRecord
*dnskey
, bool mask_revoke
) {
45 /* The algorithm from RFC 4034, Appendix B. */
48 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
50 f
= (uint32_t) dnskey
->dnskey
.flags
;
53 f
&= ~DNSKEY_FLAG_REVOKE
;
55 sum
= f
+ ((((uint32_t) dnskey
->dnskey
.protocol
) << 8) + (uint32_t) dnskey
->dnskey
.algorithm
);
57 p
= dnskey
->dnskey
.key
;
59 for (i
= 0; i
< dnskey
->dnskey
.key_size
; i
++)
60 sum
+= (i
& 1) == 0 ? (uint32_t) p
[i
] << 8 : (uint32_t) p
[i
];
62 sum
+= (sum
>> 16) & UINT32_C(0xFFFF);
64 return sum
& UINT32_C(0xFFFF);
67 int dnssec_canonicalize(const char *n
, char *buffer
, size_t buffer_max
) {
71 /* Converts the specified hostname into DNSSEC canonicalized
78 r
= dns_label_unescape(&n
, buffer
, buffer_max
, 0);
84 if (buffer_max
< (size_t) r
+ 2)
87 /* The DNSSEC canonical form is not clear on what to
88 * do with dots appearing in labels, the way DNS-SD
89 * does it. Refuse it for now. */
91 if (memchr(buffer
, '.', r
))
94 ascii_strlower_n(buffer
, (size_t) r
);
104 /* Not even a single label: this is the root domain name */
106 assert(buffer_max
> 2);
118 static int rr_compare(DnsResourceRecord
* const *a
, DnsResourceRecord
* const *b
) {
119 const DnsResourceRecord
*x
= *a
, *y
= *b
;
123 /* Let's order the RRs according to RFC 4034, Section 6.3 */
126 assert(x
->wire_format
);
128 assert(y
->wire_format
);
130 m
= MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(x
), DNS_RESOURCE_RECORD_RDATA_SIZE(y
));
132 r
= memcmp(DNS_RESOURCE_RECORD_RDATA(x
), DNS_RESOURCE_RECORD_RDATA(y
), m
);
136 return CMP(DNS_RESOURCE_RECORD_RDATA_SIZE(x
), DNS_RESOURCE_RECORD_RDATA_SIZE(y
));
139 static int dnssec_rsa_verify_raw(
140 const char *hash_algorithm
,
141 const void *signature
, size_t signature_size
,
142 const void *data
, size_t data_size
,
143 const void *exponent
, size_t exponent_size
,
144 const void *modulus
, size_t modulus_size
) {
146 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
147 gcry_mpi_t n
= NULL
, e
= NULL
, s
= NULL
;
151 assert(hash_algorithm
);
153 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature
, signature_size
, NULL
);
159 ge
= gcry_mpi_scan(&e
, GCRYMPI_FMT_USG
, exponent
, exponent_size
, NULL
);
165 ge
= gcry_mpi_scan(&n
, GCRYMPI_FMT_USG
, modulus
, modulus_size
, NULL
);
171 ge
= gcry_sexp_build(&signature_sexp
,
173 "(sig-val (rsa (s %m)))",
181 ge
= gcry_sexp_build(&data_sexp
,
183 "(data (flags pkcs1) (hash %s %b))",
192 ge
= gcry_sexp_build(&public_key_sexp
,
194 "(public-key (rsa (n %m) (e %m)))",
202 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
203 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
206 log_debug("RSA signature check failed: %s", gpg_strerror(ge
));
220 gcry_sexp_release(public_key_sexp
);
222 gcry_sexp_release(signature_sexp
);
224 gcry_sexp_release(data_sexp
);
229 static int dnssec_rsa_verify(
230 const char *hash_algorithm
,
231 const void *hash
, size_t hash_size
,
232 DnsResourceRecord
*rrsig
,
233 DnsResourceRecord
*dnskey
) {
235 size_t exponent_size
, modulus_size
;
236 void *exponent
, *modulus
;
238 assert(hash_algorithm
);
240 assert(hash_size
> 0);
244 if (*(uint8_t*) dnskey
->dnskey
.key
== 0) {
245 /* exponent is > 255 bytes long */
247 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 3;
249 ((size_t) (((uint8_t*) dnskey
->dnskey
.key
)[1]) << 8) |
250 ((size_t) ((uint8_t*) dnskey
->dnskey
.key
)[2]);
252 if (exponent_size
< 256)
255 if (3 + exponent_size
>= dnskey
->dnskey
.key_size
)
258 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 3 + exponent_size
;
259 modulus_size
= dnskey
->dnskey
.key_size
- 3 - exponent_size
;
262 /* exponent is <= 255 bytes long */
264 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 1;
265 exponent_size
= (size_t) ((uint8_t*) dnskey
->dnskey
.key
)[0];
267 if (exponent_size
<= 0)
270 if (1 + exponent_size
>= dnskey
->dnskey
.key_size
)
273 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 1 + exponent_size
;
274 modulus_size
= dnskey
->dnskey
.key_size
- 1 - exponent_size
;
277 return dnssec_rsa_verify_raw(
279 rrsig
->rrsig
.signature
, rrsig
->rrsig
.signature_size
,
281 exponent
, exponent_size
,
282 modulus
, modulus_size
);
285 static int dnssec_ecdsa_verify_raw(
286 const char *hash_algorithm
,
288 const void *signature_r
, size_t signature_r_size
,
289 const void *signature_s
, size_t signature_s_size
,
290 const void *data
, size_t data_size
,
291 const void *key
, size_t key_size
) {
293 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
294 gcry_mpi_t q
= NULL
, r
= NULL
, s
= NULL
;
298 assert(hash_algorithm
);
300 ge
= gcry_mpi_scan(&r
, GCRYMPI_FMT_USG
, signature_r
, signature_r_size
, NULL
);
306 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature_s
, signature_s_size
, NULL
);
312 ge
= gcry_mpi_scan(&q
, GCRYMPI_FMT_USG
, key
, key_size
, NULL
);
318 ge
= gcry_sexp_build(&signature_sexp
,
320 "(sig-val (ecdsa (r %m) (s %m)))",
328 ge
= gcry_sexp_build(&data_sexp
,
330 "(data (flags rfc6979) (hash %s %b))",
339 ge
= gcry_sexp_build(&public_key_sexp
,
341 "(public-key (ecc (curve %s) (q %m)))",
349 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
350 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
353 log_debug("ECDSA signature check failed: %s", gpg_strerror(ge
));
366 gcry_sexp_release(public_key_sexp
);
368 gcry_sexp_release(signature_sexp
);
370 gcry_sexp_release(data_sexp
);
375 static int dnssec_ecdsa_verify(
376 const char *hash_algorithm
,
378 const void *hash
, size_t hash_size
,
379 DnsResourceRecord
*rrsig
,
380 DnsResourceRecord
*dnskey
) {
391 if (algorithm
== DNSSEC_ALGORITHM_ECDSAP256SHA256
) {
393 curve
= "NIST P-256";
394 } else if (algorithm
== DNSSEC_ALGORITHM_ECDSAP384SHA384
) {
396 curve
= "NIST P-384";
400 if (dnskey
->dnskey
.key_size
!= key_size
* 2)
403 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
406 q
= newa(uint8_t, key_size
*2 + 1);
407 q
[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
408 memcpy(q
+1, dnskey
->dnskey
.key
, key_size
*2);
410 return dnssec_ecdsa_verify_raw(
413 rrsig
->rrsig
.signature
, key_size
,
414 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
419 #if GCRYPT_VERSION_NUMBER >= 0x010600
420 static int dnssec_eddsa_verify_raw(
422 const void *signature_r
, size_t signature_r_size
,
423 const void *signature_s
, size_t signature_s_size
,
424 const void *data
, size_t data_size
,
425 const void *key
, size_t key_size
) {
427 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
431 ge
= gcry_sexp_build(&signature_sexp
,
433 "(sig-val (eddsa (r %b) (s %b)))",
434 (int) signature_r_size
,
436 (int) signature_s_size
,
443 ge
= gcry_sexp_build(&data_sexp
,
445 "(data (flags eddsa) (hash-algo sha512) (value %b))",
453 ge
= gcry_sexp_build(&public_key_sexp
,
455 "(public-key (ecc (curve %s) (flags eddsa) (q %b)))",
464 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
465 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
468 log_debug("EdDSA signature check failed: %s", gpg_strerror(ge
));
474 gcry_sexp_release(public_key_sexp
);
476 gcry_sexp_release(signature_sexp
);
478 gcry_sexp_release(data_sexp
);
483 static int dnssec_eddsa_verify(
485 const void *data
, size_t data_size
,
486 DnsResourceRecord
*rrsig
,
487 DnsResourceRecord
*dnskey
) {
491 if (algorithm
== DNSSEC_ALGORITHM_ED25519
) {
497 if (dnskey
->dnskey
.key_size
!= key_size
)
500 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
503 return dnssec_eddsa_verify_raw(
505 rrsig
->rrsig
.signature
, key_size
,
506 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
508 dnskey
->dnskey
.key
, key_size
);
512 static void md_add_uint8(gcry_md_hd_t md
, uint8_t v
) {
513 gcry_md_write(md
, &v
, sizeof(v
));
516 static void md_add_uint16(gcry_md_hd_t md
, uint16_t v
) {
518 gcry_md_write(md
, &v
, sizeof(v
));
521 static void fwrite_uint8(FILE *fp
, uint8_t v
) {
522 fwrite(&v
, sizeof(v
), 1, fp
);
525 static void fwrite_uint16(FILE *fp
, uint16_t v
) {
527 fwrite(&v
, sizeof(v
), 1, fp
);
530 static void fwrite_uint32(FILE *fp
, uint32_t v
) {
532 fwrite(&v
, sizeof(v
), 1, fp
);
535 static int dnssec_rrsig_prepare(DnsResourceRecord
*rrsig
) {
536 int n_key_labels
, n_signer_labels
;
540 /* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source and
541 * .n_skip_labels_signer fields so that we can use them later on. */
544 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
546 /* Check if this RRSIG RR is already prepared */
547 if (rrsig
->n_skip_labels_source
!= (unsigned) -1)
550 if (rrsig
->rrsig
.inception
> rrsig
->rrsig
.expiration
)
553 name
= dns_resource_key_name(rrsig
->key
);
555 n_key_labels
= dns_name_count_labels(name
);
556 if (n_key_labels
< 0)
558 if (rrsig
->rrsig
.labels
> n_key_labels
)
561 n_signer_labels
= dns_name_count_labels(rrsig
->rrsig
.signer
);
562 if (n_signer_labels
< 0)
563 return n_signer_labels
;
564 if (n_signer_labels
> rrsig
->rrsig
.labels
)
567 r
= dns_name_skip(name
, n_key_labels
- n_signer_labels
, &name
);
573 /* Check if the signer is really a suffix of us */
574 r
= dns_name_equal(name
, rrsig
->rrsig
.signer
);
580 rrsig
->n_skip_labels_source
= n_key_labels
- rrsig
->rrsig
.labels
;
581 rrsig
->n_skip_labels_signer
= n_key_labels
- n_signer_labels
;
586 static int dnssec_rrsig_expired(DnsResourceRecord
*rrsig
, usec_t realtime
) {
587 usec_t expiration
, inception
, skew
;
590 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
592 if (realtime
== USEC_INFINITY
)
593 realtime
= now(CLOCK_REALTIME
);
595 expiration
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
596 inception
= rrsig
->rrsig
.inception
* USEC_PER_SEC
;
598 /* Consider inverted validity intervals as expired */
599 if (inception
> expiration
)
602 /* Permit a certain amount of clock skew of 10% of the valid
603 * time range. This takes inspiration from unbound's
605 skew
= (expiration
- inception
) / 10;
609 if (inception
< skew
)
614 if (expiration
+ skew
< expiration
)
615 expiration
= USEC_INFINITY
;
619 return realtime
< inception
|| realtime
> expiration
;
622 static int algorithm_to_gcrypt_md(uint8_t algorithm
) {
624 /* Translates a DNSSEC signature algorithm into a gcrypt
627 * Note that we implement all algorithms listed as "Must
628 * implement" and "Recommended to Implement" in RFC6944. We
629 * don't implement any algorithms that are listed as
630 * "Optional" or "Must Not Implement". Specifically, we do not
631 * implement RSAMD5, DSASHA1, DH, DSA-NSEC3-SHA1, and
636 case DNSSEC_ALGORITHM_RSASHA1
:
637 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
640 case DNSSEC_ALGORITHM_RSASHA256
:
641 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
642 return GCRY_MD_SHA256
;
644 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
645 return GCRY_MD_SHA384
;
647 case DNSSEC_ALGORITHM_RSASHA512
:
648 return GCRY_MD_SHA512
;
655 static void dnssec_fix_rrset_ttl(
656 DnsResourceRecord
*list
[],
658 DnsResourceRecord
*rrsig
,
667 for (k
= 0; k
< n
; k
++) {
668 DnsResourceRecord
*rr
= list
[k
];
670 /* Pick the TTL as the minimum of the RR's TTL, the
671 * RR's original TTL according to the RRSIG and the
672 * RRSIG's own TTL, see RFC 4035, Section 5.3.3 */
673 rr
->ttl
= MIN3(rr
->ttl
, rrsig
->rrsig
.original_ttl
, rrsig
->ttl
);
674 rr
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
676 /* Copy over information about the signer and wildcard source of synthesis */
677 rr
->n_skip_labels_source
= rrsig
->n_skip_labels_source
;
678 rr
->n_skip_labels_signer
= rrsig
->n_skip_labels_signer
;
681 rrsig
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
684 int dnssec_verify_rrset(
686 const DnsResourceKey
*key
,
687 DnsResourceRecord
*rrsig
,
688 DnsResourceRecord
*dnskey
,
690 DnssecResult
*result
) {
692 uint8_t wire_format_name
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
693 DnsResourceRecord
**list
, *rr
;
694 const char *source
, *name
;
695 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
699 _cleanup_free_
char *sig_data
= NULL
;
700 _cleanup_fclose_
FILE *f
= NULL
;
709 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
710 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
712 /* Verifies that the RRSet matches the specified "key" in "a",
713 * using the signature "rrsig" and the key "dnskey". It's
714 * assumed that RRSIG and DNSKEY match. */
716 r
= dnssec_rrsig_prepare(rrsig
);
718 *result
= DNSSEC_INVALID
;
724 r
= dnssec_rrsig_expired(rrsig
, realtime
);
728 *result
= DNSSEC_SIGNATURE_EXPIRED
;
732 name
= dns_resource_key_name(key
);
734 /* Some keys may only appear signed in the zone apex, and are invalid anywhere else. (SOA, NS...) */
735 if (dns_type_apex_only(rrsig
->rrsig
.type_covered
)) {
736 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
740 *result
= DNSSEC_INVALID
;
745 /* OTOH DS RRs may not appear in the zone apex, but are valid everywhere else. */
746 if (rrsig
->rrsig
.type_covered
== DNS_TYPE_DS
) {
747 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
751 *result
= DNSSEC_INVALID
;
756 /* Determine the "Source of Synthesis" and whether this is a wildcard RRSIG */
757 r
= dns_name_suffix(name
, rrsig
->rrsig
.labels
, &source
);
760 if (r
> 0 && !dns_type_may_wildcard(rrsig
->rrsig
.type_covered
)) {
761 /* We refuse to validate NSEC3 or SOA RRs that are synthesized from wildcards */
762 *result
= DNSSEC_INVALID
;
766 /* If we stripped a single label, then let's see if that maybe was "*". If so, we are not really
767 * synthesized from a wildcard, we are the wildcard itself. Treat that like a normal name. */
768 r
= dns_name_startswith(name
, "*");
778 /* Collect all relevant RRs in a single array, so that we can look at the RRset */
779 list
= newa(DnsResourceRecord
*, dns_answer_size(a
));
781 DNS_ANSWER_FOREACH(rr
, a
) {
782 r
= dns_resource_key_equal(key
, rr
->key
);
788 /* We need the wire format for ordering, and digest calculation */
789 r
= dns_resource_record_to_wire_format(rr
, true);
795 if (n
> VERIFY_RRS_MAX
)
802 /* Bring the RRs into canonical order */
803 typesafe_qsort(list
, n
, rr_compare
);
805 f
= open_memstream(&sig_data
, &sig_size
);
808 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
810 fwrite_uint16(f
, rrsig
->rrsig
.type_covered
);
811 fwrite_uint8(f
, rrsig
->rrsig
.algorithm
);
812 fwrite_uint8(f
, rrsig
->rrsig
.labels
);
813 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
814 fwrite_uint32(f
, rrsig
->rrsig
.expiration
);
815 fwrite_uint32(f
, rrsig
->rrsig
.inception
);
816 fwrite_uint16(f
, rrsig
->rrsig
.key_tag
);
818 r
= dns_name_to_wire_format(rrsig
->rrsig
.signer
, wire_format_name
, sizeof(wire_format_name
), true);
821 fwrite(wire_format_name
, 1, r
, f
);
823 /* Convert the source of synthesis into wire format */
824 r
= dns_name_to_wire_format(source
, wire_format_name
, sizeof(wire_format_name
), true);
828 for (k
= 0; k
< n
; k
++) {
833 /* Hash the source of synthesis. If this is a wildcard, then prefix it with the *. label */
835 fwrite((uint8_t[]) { 1, '*'}, sizeof(uint8_t), 2, f
);
836 fwrite(wire_format_name
, 1, r
, f
);
838 fwrite_uint16(f
, rr
->key
->type
);
839 fwrite_uint16(f
, rr
->key
->class);
840 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
842 l
= DNS_RESOURCE_RECORD_RDATA_SIZE(rr
);
845 fwrite_uint16(f
, (uint16_t) l
);
846 fwrite(DNS_RESOURCE_RECORD_RDATA(rr
), 1, l
, f
);
849 r
= fflush_and_check(f
);
853 initialize_libgcrypt(false);
855 switch (rrsig
->rrsig
.algorithm
) {
856 #if GCRYPT_VERSION_NUMBER >= 0x010600
857 case DNSSEC_ALGORITHM_ED25519
:
860 case DNSSEC_ALGORITHM_ED25519
:
862 case DNSSEC_ALGORITHM_ED448
:
863 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
866 /* OK, the RRs are now in canonical order. Let's calculate the digest */
867 md_algorithm
= algorithm_to_gcrypt_md(rrsig
->rrsig
.algorithm
);
868 if (md_algorithm
== -EOPNOTSUPP
) {
869 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
872 if (md_algorithm
< 0)
875 gcry_md_open(&md
, md_algorithm
, 0);
879 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
880 assert(hash_size
> 0);
882 gcry_md_write(md
, sig_data
, sig_size
);
884 hash
= gcry_md_read(md
, 0);
889 switch (rrsig
->rrsig
.algorithm
) {
891 case DNSSEC_ALGORITHM_RSASHA1
:
892 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
893 case DNSSEC_ALGORITHM_RSASHA256
:
894 case DNSSEC_ALGORITHM_RSASHA512
:
895 r
= dnssec_rsa_verify(
896 gcry_md_algo_name(md_algorithm
),
902 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
903 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
904 r
= dnssec_ecdsa_verify(
905 gcry_md_algo_name(md_algorithm
),
906 rrsig
->rrsig
.algorithm
,
911 #if GCRYPT_VERSION_NUMBER >= 0x010600
912 case DNSSEC_ALGORITHM_ED25519
:
913 r
= dnssec_eddsa_verify(
914 rrsig
->rrsig
.algorithm
,
924 /* Now, fix the ttl, expiry, and remember the synthesizing source and the signer */
926 dnssec_fix_rrset_ttl(list
, n
, rrsig
, realtime
);
929 *result
= DNSSEC_INVALID
;
931 *result
= DNSSEC_VALIDATED_WILDCARD
;
933 *result
= DNSSEC_VALIDATED
;
938 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
943 /* Checks if the specified DNSKEY RR matches the key used for
944 * the signature in the specified RRSIG RR */
946 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
949 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
951 if (dnskey
->key
->class != rrsig
->key
->class)
953 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
955 if (!revoked_ok
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
957 if (dnskey
->dnskey
.protocol
!= 3)
959 if (dnskey
->dnskey
.algorithm
!= rrsig
->rrsig
.algorithm
)
962 if (dnssec_keytag(dnskey
, false) != rrsig
->rrsig
.key_tag
)
965 return dns_name_equal(dns_resource_key_name(dnskey
->key
), rrsig
->rrsig
.signer
);
968 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
972 /* Checks if the specified RRSIG RR protects the RRSet of the specified RR key. */
974 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
976 if (rrsig
->key
->class != key
->class)
978 if (rrsig
->rrsig
.type_covered
!= key
->type
)
981 return dns_name_equal(dns_resource_key_name(rrsig
->key
), dns_resource_key_name(key
));
984 int dnssec_verify_rrset_search(
986 const DnsResourceKey
*key
,
987 DnsAnswer
*validated_dnskeys
,
989 DnssecResult
*result
,
990 DnsResourceRecord
**ret_rrsig
) {
992 bool found_rrsig
= false, found_invalid
= false, found_expired_rrsig
= false, found_unsupported_algorithm
= false;
993 DnsResourceRecord
*rrsig
;
999 /* Verifies all RRs from "a" that match the key "key" against DNSKEYs in "validated_dnskeys" */
1001 if (!a
|| a
->n_rrs
<= 0)
1004 /* Iterate through each RRSIG RR. */
1005 DNS_ANSWER_FOREACH(rrsig
, a
) {
1006 DnsResourceRecord
*dnskey
;
1007 DnsAnswerFlags flags
;
1009 /* Is this an RRSIG RR that applies to RRs matching our key? */
1010 r
= dnssec_key_match_rrsig(key
, rrsig
);
1018 /* Look for a matching key */
1019 DNS_ANSWER_FOREACH_FLAGS(dnskey
, flags
, validated_dnskeys
) {
1020 DnssecResult one_result
;
1022 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1025 /* Is this a DNSKEY RR that matches they key of our RRSIG? */
1026 r
= dnssec_rrsig_match_dnskey(rrsig
, dnskey
, false);
1032 /* Take the time here, if it isn't set yet, so
1033 * that we do all validations with the same
1035 if (realtime
== USEC_INFINITY
)
1036 realtime
= now(CLOCK_REALTIME
);
1038 /* Yay, we found a matching RRSIG with a matching
1039 * DNSKEY, awesome. Now let's verify all entries of
1040 * the RRSet against the RRSIG and DNSKEY
1043 r
= dnssec_verify_rrset(a
, key
, rrsig
, dnskey
, realtime
, &one_result
);
1047 switch (one_result
) {
1049 case DNSSEC_VALIDATED
:
1050 case DNSSEC_VALIDATED_WILDCARD
:
1051 /* Yay, the RR has been validated,
1052 * return immediately, but fix up the expiry */
1056 *result
= one_result
;
1059 case DNSSEC_INVALID
:
1060 /* If the signature is invalid, let's try another
1061 key and/or signature. After all they
1062 key_tags and stuff are not unique, and
1063 might be shared by multiple keys. */
1064 found_invalid
= true;
1067 case DNSSEC_UNSUPPORTED_ALGORITHM
:
1068 /* If the key algorithm is
1069 unsupported, try another
1070 RRSIG/DNSKEY pair, but remember we
1071 encountered this, so that we can
1072 return a proper error when we
1073 encounter nothing better. */
1074 found_unsupported_algorithm
= true;
1077 case DNSSEC_SIGNATURE_EXPIRED
:
1078 /* If the signature is expired, try
1079 another one, but remember it, so
1080 that we can return this */
1081 found_expired_rrsig
= true;
1085 assert_not_reached("Unexpected DNSSEC validation result");
1090 if (found_expired_rrsig
)
1091 *result
= DNSSEC_SIGNATURE_EXPIRED
;
1092 else if (found_unsupported_algorithm
)
1093 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
1094 else if (found_invalid
)
1095 *result
= DNSSEC_INVALID
;
1096 else if (found_rrsig
)
1097 *result
= DNSSEC_MISSING_KEY
;
1099 *result
= DNSSEC_NO_SIGNATURE
;
1107 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
1108 DnsResourceRecord
*rr
;
1111 /* Checks whether there's at least one RRSIG in 'a' that proctects RRs of the specified key */
1113 DNS_ANSWER_FOREACH(rr
, a
) {
1114 r
= dnssec_key_match_rrsig(key
, rr
);
1124 static int digest_to_gcrypt_md(uint8_t algorithm
) {
1126 /* Translates a DNSSEC digest algorithm into a gcrypt digest identifier */
1128 switch (algorithm
) {
1130 case DNSSEC_DIGEST_SHA1
:
1131 return GCRY_MD_SHA1
;
1133 case DNSSEC_DIGEST_SHA256
:
1134 return GCRY_MD_SHA256
;
1136 case DNSSEC_DIGEST_SHA384
:
1137 return GCRY_MD_SHA384
;
1144 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
1145 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1146 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
1148 int md_algorithm
, r
;
1154 /* Implements DNSKEY verification by a DS, according to RFC 4035, section 5.2 */
1156 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1158 if (ds
->key
->type
!= DNS_TYPE_DS
)
1160 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
1161 return -EKEYREJECTED
;
1162 if (!mask_revoke
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
1163 return -EKEYREJECTED
;
1164 if (dnskey
->dnskey
.protocol
!= 3)
1165 return -EKEYREJECTED
;
1167 if (dnskey
->dnskey
.algorithm
!= ds
->ds
.algorithm
)
1169 if (dnssec_keytag(dnskey
, mask_revoke
) != ds
->ds
.key_tag
)
1172 initialize_libgcrypt(false);
1174 md_algorithm
= digest_to_gcrypt_md(ds
->ds
.digest_type
);
1175 if (md_algorithm
< 0)
1176 return md_algorithm
;
1178 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
1179 assert(hash_size
> 0);
1181 if (ds
->ds
.digest_size
!= hash_size
)
1184 r
= dns_name_to_wire_format(dns_resource_key_name(dnskey
->key
), wire_format
, sizeof(wire_format
), true);
1188 gcry_md_open(&md
, md_algorithm
, 0);
1192 gcry_md_write(md
, wire_format
, r
);
1194 md_add_uint16(md
, dnskey
->dnskey
.flags
& ~DNSKEY_FLAG_REVOKE
);
1196 md_add_uint16(md
, dnskey
->dnskey
.flags
);
1197 md_add_uint8(md
, dnskey
->dnskey
.protocol
);
1198 md_add_uint8(md
, dnskey
->dnskey
.algorithm
);
1199 gcry_md_write(md
, dnskey
->dnskey
.key
, dnskey
->dnskey
.key_size
);
1201 result
= gcry_md_read(md
, 0);
1205 return memcmp(result
, ds
->ds
.digest
, ds
->ds
.digest_size
) == 0;
1208 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
1209 DnsResourceRecord
*ds
;
1210 DnsAnswerFlags flags
;
1215 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1218 DNS_ANSWER_FOREACH_FLAGS(ds
, flags
, validated_ds
) {
1220 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1223 if (ds
->key
->type
!= DNS_TYPE_DS
)
1225 if (ds
->key
->class != dnskey
->key
->class)
1228 r
= dns_name_equal(dns_resource_key_name(dnskey
->key
), dns_resource_key_name(ds
->key
));
1234 r
= dnssec_verify_dnskey_by_ds(dnskey
, ds
, false);
1235 if (IN_SET(r
, -EKEYREJECTED
, -EOPNOTSUPP
))
1236 return 0; /* The DNSKEY is revoked or otherwise invalid, or we don't support the digest algorithm */
1246 static int nsec3_hash_to_gcrypt_md(uint8_t algorithm
) {
1248 /* Translates a DNSSEC NSEC3 hash algorithm into a gcrypt digest identifier */
1250 switch (algorithm
) {
1252 case NSEC3_ALGORITHM_SHA1
:
1253 return GCRY_MD_SHA1
;
1260 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
1261 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1262 gcry_md_hd_t md
= NULL
;
1273 if (nsec3
->key
->type
!= DNS_TYPE_NSEC3
)
1276 if (nsec3
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1277 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1278 "Ignoring NSEC3 RR %s with excessive number of iterations.",
1279 dns_resource_record_to_string(nsec3
));
1281 algorithm
= nsec3_hash_to_gcrypt_md(nsec3
->nsec3
.algorithm
);
1285 initialize_libgcrypt(false);
1287 hash_size
= gcry_md_get_algo_dlen(algorithm
);
1288 assert(hash_size
> 0);
1290 if (nsec3
->nsec3
.next_hashed_name_size
!= hash_size
)
1293 r
= dns_name_to_wire_format(name
, wire_format
, sizeof(wire_format
), true);
1297 gcry_md_open(&md
, algorithm
, 0);
1301 gcry_md_write(md
, wire_format
, r
);
1302 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1304 result
= gcry_md_read(md
, 0);
1310 for (k
= 0; k
< nsec3
->nsec3
.iterations
; k
++) {
1311 uint8_t tmp
[hash_size
];
1312 memcpy(tmp
, result
, hash_size
);
1315 gcry_md_write(md
, tmp
, hash_size
);
1316 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1318 result
= gcry_md_read(md
, 0);
1325 memcpy(ret
, result
, hash_size
);
1326 r
= (int) hash_size
;
1333 static int nsec3_is_good(DnsResourceRecord
*rr
, DnsResourceRecord
*nsec3
) {
1339 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
1342 /* RFC 5155, Section 8.2 says we MUST ignore NSEC3 RRs with flags != 0 or 1 */
1343 if (!IN_SET(rr
->nsec3
.flags
, 0, 1))
1346 /* Ignore NSEC3 RRs whose algorithm we don't know */
1347 if (nsec3_hash_to_gcrypt_md(rr
->nsec3
.algorithm
) < 0)
1349 /* Ignore NSEC3 RRs with an excessive number of required iterations */
1350 if (rr
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1353 /* Ignore NSEC3 RRs generated from wildcards. If these NSEC3 RRs weren't correctly signed we can't make this
1354 * check (since rr->n_skip_labels_source is -1), but that's OK, as we won't trust them anyway in that case. */
1355 if (!IN_SET(rr
->n_skip_labels_source
, 0, (unsigned) -1))
1357 /* Ignore NSEC3 RRs that are located anywhere else than one label below the zone */
1358 if (!IN_SET(rr
->n_skip_labels_signer
, 1, (unsigned) -1))
1364 /* If a second NSEC3 RR is specified, also check if they are from the same zone. */
1366 if (nsec3
== rr
) /* Shortcut */
1369 if (rr
->key
->class != nsec3
->key
->class)
1371 if (rr
->nsec3
.algorithm
!= nsec3
->nsec3
.algorithm
)
1373 if (rr
->nsec3
.iterations
!= nsec3
->nsec3
.iterations
)
1375 if (rr
->nsec3
.salt_size
!= nsec3
->nsec3
.salt_size
)
1377 if (memcmp_safe(rr
->nsec3
.salt
, nsec3
->nsec3
.salt
, rr
->nsec3
.salt_size
) != 0)
1380 a
= dns_resource_key_name(rr
->key
);
1381 r
= dns_name_parent(&a
); /* strip off hash */
1385 b
= dns_resource_key_name(nsec3
->key
);
1386 r
= dns_name_parent(&b
); /* strip off hash */
1390 /* Make sure both have the same parent */
1391 return dns_name_equal(a
, b
);
1394 static int nsec3_hashed_domain_format(const uint8_t *hashed
, size_t hashed_size
, const char *zone
, char **ret
) {
1395 _cleanup_free_
char *l
= NULL
;
1399 assert(hashed_size
> 0);
1403 l
= base32hexmem(hashed
, hashed_size
, false);
1407 j
= strjoin(l
, ".", zone
);
1412 return (int) hashed_size
;
1415 static int nsec3_hashed_domain_make(DnsResourceRecord
*nsec3
, const char *domain
, const char *zone
, char **ret
) {
1416 uint8_t hashed
[DNSSEC_HASH_SIZE_MAX
];
1424 hashed_size
= dnssec_nsec3_hash(nsec3
, domain
, hashed
);
1425 if (hashed_size
< 0)
1428 return nsec3_hashed_domain_format(hashed
, (size_t) hashed_size
, zone
, ret
);
1431 /* See RFC 5155, Section 8
1432 * First try to find a NSEC3 record that matches our query precisely, if that fails, find the closest
1433 * enclosure. Secondly, find a proof that there is no closer enclosure and either a proof that there
1434 * is no wildcard domain as a direct descendant of the closest enclosure, or find an NSEC3 record that
1435 * matches the wildcard domain.
1437 * Based on this we can prove either the existence of the record in @key, or NXDOMAIN or NODATA, or
1438 * that there is no proof either way. The latter is the case if a the proof of non-existence of a given
1439 * name uses an NSEC3 record with the opt-out bit set. Lastly, if we are given insufficient NSEC3 records
1440 * to conclude anything we indicate this by returning NO_RR. */
1441 static int dnssec_test_nsec3(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1442 _cleanup_free_
char *next_closer_domain
= NULL
, *wildcard_domain
= NULL
;
1443 const char *zone
, *p
, *pp
= NULL
, *wildcard
;
1444 DnsResourceRecord
*rr
, *enclosure_rr
, *zone_rr
, *wildcard_rr
= NULL
;
1445 DnsAnswerFlags flags
;
1447 bool a
, no_closer
= false, no_wildcard
= false, optout
= false;
1452 /* First step, find the zone name and the NSEC3 parameters of the zone.
1453 * it is sufficient to look for the longest common suffix we find with
1454 * any NSEC3 RR in the response. Any NSEC3 record will do as all NSEC3
1455 * records from a given zone in a response must use the same
1457 zone
= dns_resource_key_name(key
);
1459 DNS_ANSWER_FOREACH_FLAGS(zone_rr
, flags
, answer
) {
1460 r
= nsec3_is_good(zone_rr
, NULL
);
1466 r
= dns_name_equal_skip(dns_resource_key_name(zone_rr
->key
), 1, zone
);
1473 /* Strip one label from the front */
1474 r
= dns_name_parent(&zone
);
1481 *result
= DNSSEC_NSEC_NO_RR
;
1485 /* Second step, find the closest encloser NSEC3 RR in 'answer' that matches 'key' */
1486 p
= dns_resource_key_name(key
);
1488 _cleanup_free_
char *hashed_domain
= NULL
;
1490 hashed_size
= nsec3_hashed_domain_make(zone_rr
, p
, zone
, &hashed_domain
);
1491 if (hashed_size
== -EOPNOTSUPP
) {
1492 *result
= DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
;
1495 if (hashed_size
< 0)
1498 DNS_ANSWER_FOREACH_FLAGS(enclosure_rr
, flags
, answer
) {
1500 r
= nsec3_is_good(enclosure_rr
, zone_rr
);
1506 if (enclosure_rr
->nsec3
.next_hashed_name_size
!= (size_t) hashed_size
)
1509 r
= dns_name_equal(dns_resource_key_name(enclosure_rr
->key
), hashed_domain
);
1513 a
= flags
& DNS_ANSWER_AUTHENTICATED
;
1514 goto found_closest_encloser
;
1518 /* We didn't find the closest encloser with this name,
1519 * but let's remember this domain name, it might be
1520 * the next closer name */
1524 /* Strip one label from the front */
1525 r
= dns_name_parent(&p
);
1532 *result
= DNSSEC_NSEC_NO_RR
;
1535 found_closest_encloser
:
1536 /* We found a closest encloser in 'p'; next closer is 'pp' */
1539 /* We have an exact match! If we area looking for a DS RR, then we must insist that we got the NSEC3 RR
1540 * from the parent. Otherwise the one from the child. Do so, by checking whether SOA and NS are
1541 * appropriately set. */
1543 if (key
->type
== DNS_TYPE_DS
) {
1544 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1547 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1548 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1552 /* No next closer NSEC3 RR. That means there's a direct NSEC3 RR for our key. */
1553 if (bitmap_isset(enclosure_rr
->nsec3
.types
, key
->type
))
1554 *result
= DNSSEC_NSEC_FOUND
;
1555 else if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1556 *result
= DNSSEC_NSEC_CNAME
;
1558 *result
= DNSSEC_NSEC_NODATA
;
1563 *ttl
= enclosure_rr
->ttl
;
1568 /* Ensure this is not a DNAME domain, see RFC5155, section 8.3. */
1569 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_DNAME
))
1572 /* Ensure that this data is from the delegated domain
1573 * (i.e. originates from the "lower" DNS server), and isn't
1574 * just glue records (i.e. doesn't originate from the "upper"
1576 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1577 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1580 /* Prove that there is no next closer and whether or not there is a wildcard domain. */
1582 wildcard
= strjoina("*.", p
);
1583 r
= nsec3_hashed_domain_make(enclosure_rr
, wildcard
, zone
, &wildcard_domain
);
1586 if (r
!= hashed_size
)
1589 r
= nsec3_hashed_domain_make(enclosure_rr
, pp
, zone
, &next_closer_domain
);
1592 if (r
!= hashed_size
)
1595 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1596 _cleanup_free_
char *next_hashed_domain
= NULL
;
1598 r
= nsec3_is_good(rr
, zone_rr
);
1604 r
= nsec3_hashed_domain_format(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, zone
, &next_hashed_domain
);
1608 r
= dns_name_between(dns_resource_key_name(rr
->key
), next_closer_domain
, next_hashed_domain
);
1612 if (rr
->nsec3
.flags
& 1)
1615 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1620 r
= dns_name_equal(dns_resource_key_name(rr
->key
), wildcard_domain
);
1624 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1629 r
= dns_name_between(dns_resource_key_name(rr
->key
), wildcard_domain
, next_hashed_domain
);
1633 if (rr
->nsec3
.flags
& 1)
1634 /* This only makes sense if we have a wildcard delegation, which is
1635 * very unlikely, see RFC 4592, Section 4.2, but we cannot rely on
1636 * this not happening, so hence cannot simply conclude NXDOMAIN as
1640 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1646 if (wildcard_rr
&& no_wildcard
)
1650 *result
= DNSSEC_NSEC_NO_RR
;
1655 /* A wildcard exists that matches our query. */
1657 /* This is not specified in any RFC to the best of my knowledge, but
1658 * if the next closer enclosure is covered by an opt-out NSEC3 RR
1659 * it means that we cannot prove that the source of synthesis is
1660 * correct, as there may be a closer match. */
1661 *result
= DNSSEC_NSEC_OPTOUT
;
1662 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, key
->type
))
1663 *result
= DNSSEC_NSEC_FOUND
;
1664 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1665 *result
= DNSSEC_NSEC_CNAME
;
1667 *result
= DNSSEC_NSEC_NODATA
;
1670 /* The RFC only specifies that we have to care for optout for NODATA for
1671 * DS records. However, children of an insecure opt-out delegation should
1672 * also be considered opt-out, rather than verified NXDOMAIN.
1673 * Note that we do not require a proof of wildcard non-existence if the
1674 * next closer domain is covered by an opt-out, as that would not provide
1675 * any additional information. */
1676 *result
= DNSSEC_NSEC_OPTOUT
;
1677 else if (no_wildcard
)
1678 *result
= DNSSEC_NSEC_NXDOMAIN
;
1680 *result
= DNSSEC_NSEC_NO_RR
;
1690 *ttl
= enclosure_rr
->ttl
;
1695 static int dnssec_nsec_wildcard_equal(DnsResourceRecord
*rr
, const char *name
) {
1696 char label
[DNS_LABEL_MAX
];
1701 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1703 /* Checks whether the specified RR has a name beginning in "*.", and if the rest is a suffix of our name */
1705 if (rr
->n_skip_labels_source
!= 1)
1708 n
= dns_resource_key_name(rr
->key
);
1709 r
= dns_label_unescape(&n
, label
, sizeof label
, 0);
1712 if (r
!= 1 || label
[0] != '*')
1715 return dns_name_endswith(name
, n
);
1718 static int dnssec_nsec_in_path(DnsResourceRecord
*rr
, const char *name
) {
1719 const char *nn
, *common_suffix
;
1723 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1725 /* Checks whether the specified nsec RR indicates that name is an empty non-terminal (ENT)
1727 * A couple of examples:
1729 * NSEC bar → waldo.foo.bar: indicates that foo.bar exists and is an ENT
1730 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that xoo.bar and zzz.xoo.bar exist and are ENTs
1731 * NSEC yyy.zzz.xoo.bar → bar: indicates pretty much nothing about ENTs
1734 /* First, determine parent of next domain. */
1735 nn
= rr
->nsec
.next_domain_name
;
1736 r
= dns_name_parent(&nn
);
1740 /* If the name we just determined is not equal or child of the name we are interested in, then we can't say
1741 * anything at all. */
1742 r
= dns_name_endswith(nn
, name
);
1746 /* 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. */
1747 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
1751 return dns_name_endswith(name
, common_suffix
);
1754 static int dnssec_nsec_from_parent_zone(DnsResourceRecord
*rr
, const char *name
) {
1758 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1760 /* Checks whether this NSEC originates to the parent zone or the child zone. */
1762 r
= dns_name_parent(&name
);
1766 r
= dns_name_equal(name
, dns_resource_key_name(rr
->key
));
1770 /* DNAME, and NS without SOA is an indication for a delegation. */
1771 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_DNAME
))
1774 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) && !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1780 static int dnssec_nsec_covers(DnsResourceRecord
*rr
, const char *name
) {
1785 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1787 /* Checks whether the name is covered by this NSEC RR. This means, that the name is somewhere below the NSEC's
1788 * signer name, and between the NSEC's two names. */
1790 r
= dns_resource_record_signer(rr
, &signer
);
1794 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
1798 return dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
1801 static int dnssec_nsec_generate_wildcard(DnsResourceRecord
*rr
, const char *name
, char **wc
) {
1802 const char *common_suffix1
, *common_suffix2
, *signer
;
1803 int r
, labels1
, labels2
;
1806 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1808 /* Generates "Wildcard at the Closest Encloser" for the given name and NSEC RR. */
1810 r
= dns_resource_record_signer(rr
, &signer
);
1814 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
1818 r
= dns_name_common_suffix(name
, dns_resource_key_name(rr
->key
), &common_suffix1
);
1822 r
= dns_name_common_suffix(name
, rr
->nsec
.next_domain_name
, &common_suffix2
);
1826 labels1
= dns_name_count_labels(common_suffix1
);
1830 labels2
= dns_name_count_labels(common_suffix2
);
1834 if (labels1
> labels2
)
1835 r
= dns_name_concat("*", common_suffix1
, 0, wc
);
1837 r
= dns_name_concat("*", common_suffix2
, 0, wc
);
1845 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1846 bool have_nsec3
= false, covering_rr_authenticated
= false, wildcard_rr_authenticated
= false;
1847 DnsResourceRecord
*rr
, *covering_rr
= NULL
, *wildcard_rr
= NULL
;
1848 DnsAnswerFlags flags
;
1855 /* Look for any NSEC/NSEC3 RRs that say something about the specified key. */
1857 name
= dns_resource_key_name(key
);
1859 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1861 if (rr
->key
->class != key
->class)
1864 have_nsec3
= have_nsec3
|| (rr
->key
->type
== DNS_TYPE_NSEC3
);
1866 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
1869 /* The following checks only make sense for NSEC RRs that are not expanded from a wildcard */
1870 r
= dns_resource_record_is_synthetic(rr
);
1876 /* Check if this is a direct match. If so, we have encountered a NODATA case */
1877 r
= dns_name_equal(dns_resource_key_name(rr
->key
), name
);
1881 /* If it's not a direct match, maybe it's a wild card match? */
1882 r
= dnssec_nsec_wildcard_equal(rr
, name
);
1887 if (key
->type
== DNS_TYPE_DS
) {
1888 /* If we look for a DS RR and the server sent us the NSEC RR of the child zone
1889 * we have a problem. For DS RRs we want the NSEC RR from the parent */
1890 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1893 /* For all RR types, ensure that if NS is set SOA is set too, so that we know
1894 * we got the child's NSEC. */
1895 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) &&
1896 !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1900 if (bitmap_isset(rr
->nsec
.types
, key
->type
))
1901 *result
= DNSSEC_NSEC_FOUND
;
1902 else if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_CNAME
))
1903 *result
= DNSSEC_NSEC_CNAME
;
1905 *result
= DNSSEC_NSEC_NODATA
;
1908 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1915 /* Check if the name we are looking for is an empty non-terminal within the owner or next name
1916 * of the NSEC RR. */
1917 r
= dnssec_nsec_in_path(rr
, name
);
1921 *result
= DNSSEC_NSEC_NODATA
;
1924 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1931 /* The following two "covering" checks, are not useful if the NSEC is from the parent */
1932 r
= dnssec_nsec_from_parent_zone(rr
, name
);
1938 /* Check if this NSEC RR proves the absence of an explicit RR under this name */
1939 r
= dnssec_nsec_covers(rr
, name
);
1942 if (r
> 0 && (!covering_rr
|| !covering_rr_authenticated
)) {
1944 covering_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1949 _cleanup_free_
char *wc
= NULL
;
1950 r
= dnssec_nsec_generate_wildcard(covering_rr
, name
, &wc
);
1954 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1956 if (rr
->key
->class != key
->class)
1959 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
1962 /* Check if this NSEC RR proves the nonexistence of the wildcard */
1963 r
= dnssec_nsec_covers(rr
, wc
);
1966 if (r
> 0 && (!wildcard_rr
|| !wildcard_rr_authenticated
)) {
1968 wildcard_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1973 if (covering_rr
&& wildcard_rr
) {
1974 /* If we could prove that neither the name itself, nor the wildcard at the closest encloser exists, we
1975 * proved the NXDOMAIN case. */
1976 *result
= DNSSEC_NSEC_NXDOMAIN
;
1979 *authenticated
= covering_rr_authenticated
&& wildcard_rr_authenticated
;
1981 *ttl
= MIN(covering_rr
->ttl
, wildcard_rr
->ttl
);
1986 /* OK, this was not sufficient. Let's see if NSEC3 can help. */
1988 return dnssec_test_nsec3(answer
, key
, result
, authenticated
, ttl
);
1990 /* No approproate NSEC RR found, report this. */
1991 *result
= DNSSEC_NSEC_NO_RR
;
1995 static int dnssec_nsec_test_enclosed(DnsAnswer
*answer
, uint16_t type
, const char *name
, const char *zone
, bool *authenticated
) {
1996 DnsResourceRecord
*rr
;
1997 DnsAnswerFlags flags
;
2003 /* Checks whether there's an NSEC/NSEC3 that proves that the specified 'name' is non-existing in the specified
2004 * 'zone'. The 'zone' must be a suffix of the 'name'. */
2006 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
2009 if (rr
->key
->type
!= type
&& type
!= DNS_TYPE_ANY
)
2012 switch (rr
->key
->type
) {
2016 /* We only care for NSEC RRs from the indicated zone */
2017 r
= dns_resource_record_is_signer(rr
, zone
);
2023 r
= dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
2030 case DNS_TYPE_NSEC3
: {
2031 _cleanup_free_
char *hashed_domain
= NULL
, *next_hashed_domain
= NULL
;
2033 /* We only care for NSEC3 RRs from the indicated zone */
2034 r
= dns_resource_record_is_signer(rr
, zone
);
2040 r
= nsec3_is_good(rr
, NULL
);
2046 /* Format the domain we are testing with the NSEC3 RR's hash function */
2047 r
= nsec3_hashed_domain_make(
2054 if ((size_t) r
!= rr
->nsec3
.next_hashed_name_size
)
2057 /* Format the NSEC3's next hashed name as proper domain name */
2058 r
= nsec3_hashed_domain_format(
2059 rr
->nsec3
.next_hashed_name
,
2060 rr
->nsec3
.next_hashed_name_size
,
2062 &next_hashed_domain
);
2066 r
= dns_name_between(dns_resource_key_name(rr
->key
), hashed_domain
, next_hashed_domain
);
2080 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2088 static int dnssec_test_positive_wildcard_nsec3(
2093 bool *authenticated
) {
2095 const char *next_closer
= NULL
;
2098 /* Run a positive NSEC3 wildcard proof. Specifically:
2100 * A proof that the "next closer" of the generating wildcard does not exist.
2102 * Note a key difference between the NSEC3 and NSEC versions of the proof. NSEC RRs don't have to exist for
2103 * empty non-transients. NSEC3 RRs however have to. This means it's sufficient to check if the next closer name
2104 * exists for the NSEC3 RR and we are done.
2106 * 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
2107 * c.d.e.f does not exist. */
2111 r
= dns_name_parent(&name
);
2115 r
= dns_name_equal(name
, source
);
2122 return dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC3
, next_closer
, zone
, authenticated
);
2125 static int dnssec_test_positive_wildcard_nsec(
2130 bool *_authenticated
) {
2132 bool authenticated
= true;
2135 /* Run a positive NSEC wildcard proof. Specifically:
2137 * A proof that there's neither a wildcard name nor a non-wildcard name that is a suffix of the name "name" and
2138 * a prefix of the synthesizing source "source" in the zone "zone".
2140 * See RFC 5155, Section 8.8 and RFC 4035, Section 5.3.4
2142 * 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
2143 * 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
);