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 "sort-util.h"
19 #include "string-table.h"
21 #define VERIFY_RRS_MAX 256
22 #define MAX_KEY_SIZE (32*1024)
24 /* Permit a maximum clock skew of 1h 10min. This should be enough to deal with DST confusion */
25 #define SKEW_MAX (1*USEC_PER_HOUR + 10*USEC_PER_MINUTE)
27 /* Maximum number of NSEC3 iterations we'll do. RFC5155 says 2500 shall be the maximum useful value */
28 #define NSEC3_ITERATIONS_MAX 2500
31 * The DNSSEC Chain of trust:
33 * Normal RRs are protected via RRSIG RRs in combination with DNSKEY RRs, all in the same zone
34 * DNSKEY RRs are either protected like normal RRs, or via a DS from a zone "higher" up the tree
35 * DS RRs are protected like normal RRs
38 * Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS
41 uint16_t dnssec_keytag(DnsResourceRecord
*dnskey
, bool mask_revoke
) {
46 /* The algorithm from RFC 4034, Appendix B. */
49 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
51 f
= (uint32_t) dnskey
->dnskey
.flags
;
54 f
&= ~DNSKEY_FLAG_REVOKE
;
56 sum
= f
+ ((((uint32_t) dnskey
->dnskey
.protocol
) << 8) + (uint32_t) dnskey
->dnskey
.algorithm
);
58 p
= dnskey
->dnskey
.key
;
60 for (i
= 0; i
< dnskey
->dnskey
.key_size
; i
++)
61 sum
+= (i
& 1) == 0 ? (uint32_t) p
[i
] << 8 : (uint32_t) p
[i
];
63 sum
+= (sum
>> 16) & UINT32_C(0xFFFF);
65 return sum
& UINT32_C(0xFFFF);
68 int dnssec_canonicalize(const char *n
, char *buffer
, size_t buffer_max
) {
72 /* Converts the specified hostname into DNSSEC canonicalized
79 r
= dns_label_unescape(&n
, buffer
, buffer_max
, 0);
85 if (buffer_max
< (size_t) r
+ 2)
88 /* The DNSSEC canonical form is not clear on what to
89 * do with dots appearing in labels, the way DNS-SD
90 * does it. Refuse it for now. */
92 if (memchr(buffer
, '.', r
))
95 ascii_strlower_n(buffer
, (size_t) r
);
105 /* Not even a single label: this is the root domain name */
107 assert(buffer_max
> 2);
119 static int rr_compare(DnsResourceRecord
* const *a
, DnsResourceRecord
* const *b
) {
120 const DnsResourceRecord
*x
= *a
, *y
= *b
;
124 /* Let's order the RRs according to RFC 4034, Section 6.3 */
127 assert(x
->wire_format
);
129 assert(y
->wire_format
);
131 m
= MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(x
), DNS_RESOURCE_RECORD_RDATA_SIZE(y
));
133 r
= memcmp(DNS_RESOURCE_RECORD_RDATA(x
), DNS_RESOURCE_RECORD_RDATA(y
), m
);
137 return CMP(DNS_RESOURCE_RECORD_RDATA_SIZE(x
), DNS_RESOURCE_RECORD_RDATA_SIZE(y
));
140 static int dnssec_rsa_verify_raw(
141 const char *hash_algorithm
,
142 const void *signature
, size_t signature_size
,
143 const void *data
, size_t data_size
,
144 const void *exponent
, size_t exponent_size
,
145 const void *modulus
, size_t modulus_size
) {
147 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
148 gcry_mpi_t n
= NULL
, e
= NULL
, s
= NULL
;
152 assert(hash_algorithm
);
154 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature
, signature_size
, NULL
);
160 ge
= gcry_mpi_scan(&e
, GCRYMPI_FMT_USG
, exponent
, exponent_size
, NULL
);
166 ge
= gcry_mpi_scan(&n
, GCRYMPI_FMT_USG
, modulus
, modulus_size
, NULL
);
172 ge
= gcry_sexp_build(&signature_sexp
,
174 "(sig-val (rsa (s %m)))",
182 ge
= gcry_sexp_build(&data_sexp
,
184 "(data (flags pkcs1) (hash %s %b))",
193 ge
= gcry_sexp_build(&public_key_sexp
,
195 "(public-key (rsa (n %m) (e %m)))",
203 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
204 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
207 log_debug("RSA signature check failed: %s", gpg_strerror(ge
));
221 gcry_sexp_release(public_key_sexp
);
223 gcry_sexp_release(signature_sexp
);
225 gcry_sexp_release(data_sexp
);
230 static int dnssec_rsa_verify(
231 const char *hash_algorithm
,
232 const void *hash
, size_t hash_size
,
233 DnsResourceRecord
*rrsig
,
234 DnsResourceRecord
*dnskey
) {
236 size_t exponent_size
, modulus_size
;
237 void *exponent
, *modulus
;
239 assert(hash_algorithm
);
241 assert(hash_size
> 0);
245 if (*(uint8_t*) dnskey
->dnskey
.key
== 0) {
246 /* exponent is > 255 bytes long */
248 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 3;
250 ((size_t) (((uint8_t*) dnskey
->dnskey
.key
)[1]) << 8) |
251 ((size_t) ((uint8_t*) dnskey
->dnskey
.key
)[2]);
253 if (exponent_size
< 256)
256 if (3 + exponent_size
>= dnskey
->dnskey
.key_size
)
259 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 3 + exponent_size
;
260 modulus_size
= dnskey
->dnskey
.key_size
- 3 - exponent_size
;
263 /* exponent is <= 255 bytes long */
265 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 1;
266 exponent_size
= (size_t) ((uint8_t*) dnskey
->dnskey
.key
)[0];
268 if (exponent_size
<= 0)
271 if (1 + exponent_size
>= dnskey
->dnskey
.key_size
)
274 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 1 + exponent_size
;
275 modulus_size
= dnskey
->dnskey
.key_size
- 1 - exponent_size
;
278 return dnssec_rsa_verify_raw(
280 rrsig
->rrsig
.signature
, rrsig
->rrsig
.signature_size
,
282 exponent
, exponent_size
,
283 modulus
, modulus_size
);
286 static int dnssec_ecdsa_verify_raw(
287 const char *hash_algorithm
,
289 const void *signature_r
, size_t signature_r_size
,
290 const void *signature_s
, size_t signature_s_size
,
291 const void *data
, size_t data_size
,
292 const void *key
, size_t key_size
) {
294 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
295 gcry_mpi_t q
= NULL
, r
= NULL
, s
= NULL
;
299 assert(hash_algorithm
);
301 ge
= gcry_mpi_scan(&r
, GCRYMPI_FMT_USG
, signature_r
, signature_r_size
, NULL
);
307 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature_s
, signature_s_size
, NULL
);
313 ge
= gcry_mpi_scan(&q
, GCRYMPI_FMT_USG
, key
, key_size
, NULL
);
319 ge
= gcry_sexp_build(&signature_sexp
,
321 "(sig-val (ecdsa (r %m) (s %m)))",
329 ge
= gcry_sexp_build(&data_sexp
,
331 "(data (flags rfc6979) (hash %s %b))",
340 ge
= gcry_sexp_build(&public_key_sexp
,
342 "(public-key (ecc (curve %s) (q %m)))",
350 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
351 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
354 log_debug("ECDSA signature check failed: %s", gpg_strerror(ge
));
367 gcry_sexp_release(public_key_sexp
);
369 gcry_sexp_release(signature_sexp
);
371 gcry_sexp_release(data_sexp
);
376 static int dnssec_ecdsa_verify(
377 const char *hash_algorithm
,
379 const void *hash
, size_t hash_size
,
380 DnsResourceRecord
*rrsig
,
381 DnsResourceRecord
*dnskey
) {
392 if (algorithm
== DNSSEC_ALGORITHM_ECDSAP256SHA256
) {
394 curve
= "NIST P-256";
395 } else if (algorithm
== DNSSEC_ALGORITHM_ECDSAP384SHA384
) {
397 curve
= "NIST P-384";
401 if (dnskey
->dnskey
.key_size
!= key_size
* 2)
404 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
407 q
= newa(uint8_t, key_size
*2 + 1);
408 q
[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
409 memcpy(q
+1, dnskey
->dnskey
.key
, key_size
*2);
411 return dnssec_ecdsa_verify_raw(
414 rrsig
->rrsig
.signature
, key_size
,
415 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
420 #if GCRYPT_VERSION_NUMBER >= 0x010600
421 static int dnssec_eddsa_verify_raw(
423 const void *signature_r
, size_t signature_r_size
,
424 const void *signature_s
, size_t signature_s_size
,
425 const void *data
, size_t data_size
,
426 const void *key
, size_t key_size
) {
428 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
432 ge
= gcry_sexp_build(&signature_sexp
,
434 "(sig-val (eddsa (r %b) (s %b)))",
435 (int) signature_r_size
,
437 (int) signature_s_size
,
444 ge
= gcry_sexp_build(&data_sexp
,
446 "(data (flags eddsa) (hash-algo sha512) (value %b))",
454 ge
= gcry_sexp_build(&public_key_sexp
,
456 "(public-key (ecc (curve %s) (flags eddsa) (q %b)))",
465 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
466 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
469 log_debug("EdDSA signature check failed: %s", gpg_strerror(ge
));
475 gcry_sexp_release(public_key_sexp
);
477 gcry_sexp_release(signature_sexp
);
479 gcry_sexp_release(data_sexp
);
484 static int dnssec_eddsa_verify(
486 const void *data
, size_t data_size
,
487 DnsResourceRecord
*rrsig
,
488 DnsResourceRecord
*dnskey
) {
492 if (algorithm
== DNSSEC_ALGORITHM_ED25519
) {
498 if (dnskey
->dnskey
.key_size
!= key_size
)
501 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
504 return dnssec_eddsa_verify_raw(
506 rrsig
->rrsig
.signature
, key_size
,
507 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
509 dnskey
->dnskey
.key
, key_size
);
513 static void md_add_uint8(gcry_md_hd_t md
, uint8_t v
) {
514 gcry_md_write(md
, &v
, sizeof(v
));
517 static void md_add_uint16(gcry_md_hd_t md
, uint16_t v
) {
519 gcry_md_write(md
, &v
, sizeof(v
));
522 static void fwrite_uint8(FILE *fp
, uint8_t v
) {
523 fwrite(&v
, sizeof(v
), 1, fp
);
526 static void fwrite_uint16(FILE *fp
, uint16_t v
) {
528 fwrite(&v
, sizeof(v
), 1, fp
);
531 static void fwrite_uint32(FILE *fp
, uint32_t v
) {
533 fwrite(&v
, sizeof(v
), 1, fp
);
536 static int dnssec_rrsig_prepare(DnsResourceRecord
*rrsig
) {
537 int n_key_labels
, n_signer_labels
;
541 /* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source and
542 * .n_skip_labels_signer fields so that we can use them later on. */
545 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
547 /* Check if this RRSIG RR is already prepared */
548 if (rrsig
->n_skip_labels_source
!= (unsigned) -1)
551 if (rrsig
->rrsig
.inception
> rrsig
->rrsig
.expiration
)
554 name
= dns_resource_key_name(rrsig
->key
);
556 n_key_labels
= dns_name_count_labels(name
);
557 if (n_key_labels
< 0)
559 if (rrsig
->rrsig
.labels
> n_key_labels
)
562 n_signer_labels
= dns_name_count_labels(rrsig
->rrsig
.signer
);
563 if (n_signer_labels
< 0)
564 return n_signer_labels
;
565 if (n_signer_labels
> rrsig
->rrsig
.labels
)
568 r
= dns_name_skip(name
, n_key_labels
- n_signer_labels
, &name
);
574 /* Check if the signer is really a suffix of us */
575 r
= dns_name_equal(name
, rrsig
->rrsig
.signer
);
581 rrsig
->n_skip_labels_source
= n_key_labels
- rrsig
->rrsig
.labels
;
582 rrsig
->n_skip_labels_signer
= n_key_labels
- n_signer_labels
;
587 static int dnssec_rrsig_expired(DnsResourceRecord
*rrsig
, usec_t realtime
) {
588 usec_t expiration
, inception
, skew
;
591 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
593 if (realtime
== USEC_INFINITY
)
594 realtime
= now(CLOCK_REALTIME
);
596 expiration
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
597 inception
= rrsig
->rrsig
.inception
* USEC_PER_SEC
;
599 /* Consider inverted validity intervals as expired */
600 if (inception
> expiration
)
603 /* Permit a certain amount of clock skew of 10% of the valid
604 * time range. This takes inspiration from unbound's
606 skew
= (expiration
- inception
) / 10;
610 if (inception
< skew
)
615 if (expiration
+ skew
< expiration
)
616 expiration
= USEC_INFINITY
;
620 return realtime
< inception
|| realtime
> expiration
;
623 static int algorithm_to_gcrypt_md(uint8_t algorithm
) {
625 /* Translates a DNSSEC signature algorithm into a gcrypt
628 * Note that we implement all algorithms listed as "Must
629 * implement" and "Recommended to Implement" in RFC6944. We
630 * don't implement any algorithms that are listed as
631 * "Optional" or "Must Not Implement". Specifically, we do not
632 * implement RSAMD5, DSASHA1, DH, DSA-NSEC3-SHA1, and
637 case DNSSEC_ALGORITHM_RSASHA1
:
638 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
641 case DNSSEC_ALGORITHM_RSASHA256
:
642 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
643 return GCRY_MD_SHA256
;
645 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
646 return GCRY_MD_SHA384
;
648 case DNSSEC_ALGORITHM_RSASHA512
:
649 return GCRY_MD_SHA512
;
656 static void dnssec_fix_rrset_ttl(
657 DnsResourceRecord
*list
[],
659 DnsResourceRecord
*rrsig
,
668 for (k
= 0; k
< n
; k
++) {
669 DnsResourceRecord
*rr
= list
[k
];
671 /* Pick the TTL as the minimum of the RR's TTL, the
672 * RR's original TTL according to the RRSIG and the
673 * RRSIG's own TTL, see RFC 4035, Section 5.3.3 */
674 rr
->ttl
= MIN3(rr
->ttl
, rrsig
->rrsig
.original_ttl
, rrsig
->ttl
);
675 rr
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
677 /* Copy over information about the signer and wildcard source of synthesis */
678 rr
->n_skip_labels_source
= rrsig
->n_skip_labels_source
;
679 rr
->n_skip_labels_signer
= rrsig
->n_skip_labels_signer
;
682 rrsig
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
685 int dnssec_verify_rrset(
687 const DnsResourceKey
*key
,
688 DnsResourceRecord
*rrsig
,
689 DnsResourceRecord
*dnskey
,
691 DnssecResult
*result
) {
693 uint8_t wire_format_name
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
694 DnsResourceRecord
**list
, *rr
;
695 const char *source
, *name
;
696 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
700 _cleanup_free_
char *sig_data
= NULL
;
701 _cleanup_fclose_
FILE *f
= NULL
;
710 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
711 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
713 /* Verifies that the RRSet matches the specified "key" in "a",
714 * using the signature "rrsig" and the key "dnskey". It's
715 * assumed that RRSIG and DNSKEY match. */
717 r
= dnssec_rrsig_prepare(rrsig
);
719 *result
= DNSSEC_INVALID
;
725 r
= dnssec_rrsig_expired(rrsig
, realtime
);
729 *result
= DNSSEC_SIGNATURE_EXPIRED
;
733 name
= dns_resource_key_name(key
);
735 /* Some keys may only appear signed in the zone apex, and are invalid anywhere else. (SOA, NS...) */
736 if (dns_type_apex_only(rrsig
->rrsig
.type_covered
)) {
737 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
741 *result
= DNSSEC_INVALID
;
746 /* OTOH DS RRs may not appear in the zone apex, but are valid everywhere else. */
747 if (rrsig
->rrsig
.type_covered
== DNS_TYPE_DS
) {
748 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
752 *result
= DNSSEC_INVALID
;
757 /* Determine the "Source of Synthesis" and whether this is a wildcard RRSIG */
758 r
= dns_name_suffix(name
, rrsig
->rrsig
.labels
, &source
);
761 if (r
> 0 && !dns_type_may_wildcard(rrsig
->rrsig
.type_covered
)) {
762 /* We refuse to validate NSEC3 or SOA RRs that are synthesized from wildcards */
763 *result
= DNSSEC_INVALID
;
767 /* If we stripped a single label, then let's see if that maybe was "*". If so, we are not really
768 * synthesized from a wildcard, we are the wildcard itself. Treat that like a normal name. */
769 r
= dns_name_startswith(name
, "*");
779 /* Collect all relevant RRs in a single array, so that we can look at the RRset */
780 list
= newa(DnsResourceRecord
*, dns_answer_size(a
));
782 DNS_ANSWER_FOREACH(rr
, a
) {
783 r
= dns_resource_key_equal(key
, rr
->key
);
789 /* We need the wire format for ordering, and digest calculation */
790 r
= dns_resource_record_to_wire_format(rr
, true);
796 if (n
> VERIFY_RRS_MAX
)
803 /* Bring the RRs into canonical order */
804 typesafe_qsort(list
, n
, rr_compare
);
806 f
= open_memstream(&sig_data
, &sig_size
);
809 (void) __fsetlocking(f
, FSETLOCKING_BYCALLER
);
811 fwrite_uint16(f
, rrsig
->rrsig
.type_covered
);
812 fwrite_uint8(f
, rrsig
->rrsig
.algorithm
);
813 fwrite_uint8(f
, rrsig
->rrsig
.labels
);
814 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
815 fwrite_uint32(f
, rrsig
->rrsig
.expiration
);
816 fwrite_uint32(f
, rrsig
->rrsig
.inception
);
817 fwrite_uint16(f
, rrsig
->rrsig
.key_tag
);
819 r
= dns_name_to_wire_format(rrsig
->rrsig
.signer
, wire_format_name
, sizeof(wire_format_name
), true);
822 fwrite(wire_format_name
, 1, r
, f
);
824 /* Convert the source of synthesis into wire format */
825 r
= dns_name_to_wire_format(source
, wire_format_name
, sizeof(wire_format_name
), true);
829 for (k
= 0; k
< n
; k
++) {
834 /* Hash the source of synthesis. If this is a wildcard, then prefix it with the *. label */
836 fwrite((uint8_t[]) { 1, '*'}, sizeof(uint8_t), 2, f
);
837 fwrite(wire_format_name
, 1, r
, f
);
839 fwrite_uint16(f
, rr
->key
->type
);
840 fwrite_uint16(f
, rr
->key
->class);
841 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
843 l
= DNS_RESOURCE_RECORD_RDATA_SIZE(rr
);
846 fwrite_uint16(f
, (uint16_t) l
);
847 fwrite(DNS_RESOURCE_RECORD_RDATA(rr
), 1, l
, f
);
850 r
= fflush_and_check(f
);
854 initialize_libgcrypt(false);
856 switch (rrsig
->rrsig
.algorithm
) {
857 #if GCRYPT_VERSION_NUMBER >= 0x010600
858 case DNSSEC_ALGORITHM_ED25519
:
861 case DNSSEC_ALGORITHM_ED25519
:
863 case DNSSEC_ALGORITHM_ED448
:
864 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
867 /* OK, the RRs are now in canonical order. Let's calculate the digest */
868 md_algorithm
= algorithm_to_gcrypt_md(rrsig
->rrsig
.algorithm
);
869 if (md_algorithm
== -EOPNOTSUPP
) {
870 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
873 if (md_algorithm
< 0)
876 gcry_md_open(&md
, md_algorithm
, 0);
880 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
881 assert(hash_size
> 0);
883 gcry_md_write(md
, sig_data
, sig_size
);
885 hash
= gcry_md_read(md
, 0);
890 switch (rrsig
->rrsig
.algorithm
) {
892 case DNSSEC_ALGORITHM_RSASHA1
:
893 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
894 case DNSSEC_ALGORITHM_RSASHA256
:
895 case DNSSEC_ALGORITHM_RSASHA512
:
896 r
= dnssec_rsa_verify(
897 gcry_md_algo_name(md_algorithm
),
903 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
904 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
905 r
= dnssec_ecdsa_verify(
906 gcry_md_algo_name(md_algorithm
),
907 rrsig
->rrsig
.algorithm
,
912 #if GCRYPT_VERSION_NUMBER >= 0x010600
913 case DNSSEC_ALGORITHM_ED25519
:
914 r
= dnssec_eddsa_verify(
915 rrsig
->rrsig
.algorithm
,
925 /* Now, fix the ttl, expiry, and remember the synthesizing source and the signer */
927 dnssec_fix_rrset_ttl(list
, n
, rrsig
, realtime
);
930 *result
= DNSSEC_INVALID
;
932 *result
= DNSSEC_VALIDATED_WILDCARD
;
934 *result
= DNSSEC_VALIDATED
;
939 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
944 /* Checks if the specified DNSKEY RR matches the key used for
945 * the signature in the specified RRSIG RR */
947 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
950 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
952 if (dnskey
->key
->class != rrsig
->key
->class)
954 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
956 if (!revoked_ok
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
958 if (dnskey
->dnskey
.protocol
!= 3)
960 if (dnskey
->dnskey
.algorithm
!= rrsig
->rrsig
.algorithm
)
963 if (dnssec_keytag(dnskey
, false) != rrsig
->rrsig
.key_tag
)
966 return dns_name_equal(dns_resource_key_name(dnskey
->key
), rrsig
->rrsig
.signer
);
969 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
973 /* Checks if the specified RRSIG RR protects the RRSet of the specified RR key. */
975 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
977 if (rrsig
->key
->class != key
->class)
979 if (rrsig
->rrsig
.type_covered
!= key
->type
)
982 return dns_name_equal(dns_resource_key_name(rrsig
->key
), dns_resource_key_name(key
));
985 int dnssec_verify_rrset_search(
987 const DnsResourceKey
*key
,
988 DnsAnswer
*validated_dnskeys
,
990 DnssecResult
*result
,
991 DnsResourceRecord
**ret_rrsig
) {
993 bool found_rrsig
= false, found_invalid
= false, found_expired_rrsig
= false, found_unsupported_algorithm
= false;
994 DnsResourceRecord
*rrsig
;
1000 /* Verifies all RRs from "a" that match the key "key" against DNSKEYs in "validated_dnskeys" */
1002 if (!a
|| a
->n_rrs
<= 0)
1005 /* Iterate through each RRSIG RR. */
1006 DNS_ANSWER_FOREACH(rrsig
, a
) {
1007 DnsResourceRecord
*dnskey
;
1008 DnsAnswerFlags flags
;
1010 /* Is this an RRSIG RR that applies to RRs matching our key? */
1011 r
= dnssec_key_match_rrsig(key
, rrsig
);
1019 /* Look for a matching key */
1020 DNS_ANSWER_FOREACH_FLAGS(dnskey
, flags
, validated_dnskeys
) {
1021 DnssecResult one_result
;
1023 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1026 /* Is this a DNSKEY RR that matches they key of our RRSIG? */
1027 r
= dnssec_rrsig_match_dnskey(rrsig
, dnskey
, false);
1033 /* Take the time here, if it isn't set yet, so
1034 * that we do all validations with the same
1036 if (realtime
== USEC_INFINITY
)
1037 realtime
= now(CLOCK_REALTIME
);
1039 /* Yay, we found a matching RRSIG with a matching
1040 * DNSKEY, awesome. Now let's verify all entries of
1041 * the RRSet against the RRSIG and DNSKEY
1044 r
= dnssec_verify_rrset(a
, key
, rrsig
, dnskey
, realtime
, &one_result
);
1048 switch (one_result
) {
1050 case DNSSEC_VALIDATED
:
1051 case DNSSEC_VALIDATED_WILDCARD
:
1052 /* Yay, the RR has been validated,
1053 * return immediately, but fix up the expiry */
1057 *result
= one_result
;
1060 case DNSSEC_INVALID
:
1061 /* If the signature is invalid, let's try another
1062 key and/or signature. After all they
1063 key_tags and stuff are not unique, and
1064 might be shared by multiple keys. */
1065 found_invalid
= true;
1068 case DNSSEC_UNSUPPORTED_ALGORITHM
:
1069 /* If the key algorithm is
1070 unsupported, try another
1071 RRSIG/DNSKEY pair, but remember we
1072 encountered this, so that we can
1073 return a proper error when we
1074 encounter nothing better. */
1075 found_unsupported_algorithm
= true;
1078 case DNSSEC_SIGNATURE_EXPIRED
:
1079 /* If the signature is expired, try
1080 another one, but remember it, so
1081 that we can return this */
1082 found_expired_rrsig
= true;
1086 assert_not_reached("Unexpected DNSSEC validation result");
1091 if (found_expired_rrsig
)
1092 *result
= DNSSEC_SIGNATURE_EXPIRED
;
1093 else if (found_unsupported_algorithm
)
1094 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
1095 else if (found_invalid
)
1096 *result
= DNSSEC_INVALID
;
1097 else if (found_rrsig
)
1098 *result
= DNSSEC_MISSING_KEY
;
1100 *result
= DNSSEC_NO_SIGNATURE
;
1108 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
1109 DnsResourceRecord
*rr
;
1112 /* Checks whether there's at least one RRSIG in 'a' that proctects RRs of the specified key */
1114 DNS_ANSWER_FOREACH(rr
, a
) {
1115 r
= dnssec_key_match_rrsig(key
, rr
);
1125 static int digest_to_gcrypt_md(uint8_t algorithm
) {
1127 /* Translates a DNSSEC digest algorithm into a gcrypt digest identifier */
1129 switch (algorithm
) {
1131 case DNSSEC_DIGEST_SHA1
:
1132 return GCRY_MD_SHA1
;
1134 case DNSSEC_DIGEST_SHA256
:
1135 return GCRY_MD_SHA256
;
1137 case DNSSEC_DIGEST_SHA384
:
1138 return GCRY_MD_SHA384
;
1145 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
1146 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1147 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
1149 int md_algorithm
, r
;
1155 /* Implements DNSKEY verification by a DS, according to RFC 4035, section 5.2 */
1157 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1159 if (ds
->key
->type
!= DNS_TYPE_DS
)
1161 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
1162 return -EKEYREJECTED
;
1163 if (!mask_revoke
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
1164 return -EKEYREJECTED
;
1165 if (dnskey
->dnskey
.protocol
!= 3)
1166 return -EKEYREJECTED
;
1168 if (dnskey
->dnskey
.algorithm
!= ds
->ds
.algorithm
)
1170 if (dnssec_keytag(dnskey
, mask_revoke
) != ds
->ds
.key_tag
)
1173 initialize_libgcrypt(false);
1175 md_algorithm
= digest_to_gcrypt_md(ds
->ds
.digest_type
);
1176 if (md_algorithm
< 0)
1177 return md_algorithm
;
1179 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
1180 assert(hash_size
> 0);
1182 if (ds
->ds
.digest_size
!= hash_size
)
1185 r
= dns_name_to_wire_format(dns_resource_key_name(dnskey
->key
), wire_format
, sizeof(wire_format
), true);
1189 gcry_md_open(&md
, md_algorithm
, 0);
1193 gcry_md_write(md
, wire_format
, r
);
1195 md_add_uint16(md
, dnskey
->dnskey
.flags
& ~DNSKEY_FLAG_REVOKE
);
1197 md_add_uint16(md
, dnskey
->dnskey
.flags
);
1198 md_add_uint8(md
, dnskey
->dnskey
.protocol
);
1199 md_add_uint8(md
, dnskey
->dnskey
.algorithm
);
1200 gcry_md_write(md
, dnskey
->dnskey
.key
, dnskey
->dnskey
.key_size
);
1202 result
= gcry_md_read(md
, 0);
1206 return memcmp(result
, ds
->ds
.digest
, ds
->ds
.digest_size
) == 0;
1209 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
1210 DnsResourceRecord
*ds
;
1211 DnsAnswerFlags flags
;
1216 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1219 DNS_ANSWER_FOREACH_FLAGS(ds
, flags
, validated_ds
) {
1221 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1224 if (ds
->key
->type
!= DNS_TYPE_DS
)
1226 if (ds
->key
->class != dnskey
->key
->class)
1229 r
= dns_name_equal(dns_resource_key_name(dnskey
->key
), dns_resource_key_name(ds
->key
));
1235 r
= dnssec_verify_dnskey_by_ds(dnskey
, ds
, false);
1236 if (IN_SET(r
, -EKEYREJECTED
, -EOPNOTSUPP
))
1237 return 0; /* The DNSKEY is revoked or otherwise invalid, or we don't support the digest algorithm */
1247 static int nsec3_hash_to_gcrypt_md(uint8_t algorithm
) {
1249 /* Translates a DNSSEC NSEC3 hash algorithm into a gcrypt digest identifier */
1251 switch (algorithm
) {
1253 case NSEC3_ALGORITHM_SHA1
:
1254 return GCRY_MD_SHA1
;
1261 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
1262 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1263 gcry_md_hd_t md
= NULL
;
1274 if (nsec3
->key
->type
!= DNS_TYPE_NSEC3
)
1277 if (nsec3
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1278 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1279 "Ignoring NSEC3 RR %s with excessive number of iterations.",
1280 dns_resource_record_to_string(nsec3
));
1282 algorithm
= nsec3_hash_to_gcrypt_md(nsec3
->nsec3
.algorithm
);
1286 initialize_libgcrypt(false);
1288 hash_size
= gcry_md_get_algo_dlen(algorithm
);
1289 assert(hash_size
> 0);
1291 if (nsec3
->nsec3
.next_hashed_name_size
!= hash_size
)
1294 r
= dns_name_to_wire_format(name
, wire_format
, sizeof(wire_format
), true);
1298 gcry_md_open(&md
, algorithm
, 0);
1302 gcry_md_write(md
, wire_format
, r
);
1303 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1305 result
= gcry_md_read(md
, 0);
1311 for (k
= 0; k
< nsec3
->nsec3
.iterations
; k
++) {
1312 uint8_t tmp
[hash_size
];
1313 memcpy(tmp
, result
, hash_size
);
1316 gcry_md_write(md
, tmp
, hash_size
);
1317 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1319 result
= gcry_md_read(md
, 0);
1326 memcpy(ret
, result
, hash_size
);
1327 r
= (int) hash_size
;
1334 static int nsec3_is_good(DnsResourceRecord
*rr
, DnsResourceRecord
*nsec3
) {
1340 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
1343 /* RFC 5155, Section 8.2 says we MUST ignore NSEC3 RRs with flags != 0 or 1 */
1344 if (!IN_SET(rr
->nsec3
.flags
, 0, 1))
1347 /* Ignore NSEC3 RRs whose algorithm we don't know */
1348 if (nsec3_hash_to_gcrypt_md(rr
->nsec3
.algorithm
) < 0)
1350 /* Ignore NSEC3 RRs with an excessive number of required iterations */
1351 if (rr
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1354 /* Ignore NSEC3 RRs generated from wildcards. If these NSEC3 RRs weren't correctly signed we can't make this
1355 * check (since rr->n_skip_labels_source is -1), but that's OK, as we won't trust them anyway in that case. */
1356 if (!IN_SET(rr
->n_skip_labels_source
, 0, (unsigned) -1))
1358 /* Ignore NSEC3 RRs that are located anywhere else than one label below the zone */
1359 if (!IN_SET(rr
->n_skip_labels_signer
, 1, (unsigned) -1))
1365 /* If a second NSEC3 RR is specified, also check if they are from the same zone. */
1367 if (nsec3
== rr
) /* Shortcut */
1370 if (rr
->key
->class != nsec3
->key
->class)
1372 if (rr
->nsec3
.algorithm
!= nsec3
->nsec3
.algorithm
)
1374 if (rr
->nsec3
.iterations
!= nsec3
->nsec3
.iterations
)
1376 if (rr
->nsec3
.salt_size
!= nsec3
->nsec3
.salt_size
)
1378 if (memcmp_safe(rr
->nsec3
.salt
, nsec3
->nsec3
.salt
, rr
->nsec3
.salt_size
) != 0)
1381 a
= dns_resource_key_name(rr
->key
);
1382 r
= dns_name_parent(&a
); /* strip off hash */
1386 b
= dns_resource_key_name(nsec3
->key
);
1387 r
= dns_name_parent(&b
); /* strip off hash */
1391 /* Make sure both have the same parent */
1392 return dns_name_equal(a
, b
);
1395 static int nsec3_hashed_domain_format(const uint8_t *hashed
, size_t hashed_size
, const char *zone
, char **ret
) {
1396 _cleanup_free_
char *l
= NULL
;
1400 assert(hashed_size
> 0);
1404 l
= base32hexmem(hashed
, hashed_size
, false);
1408 j
= strjoin(l
, ".", zone
);
1413 return (int) hashed_size
;
1416 static int nsec3_hashed_domain_make(DnsResourceRecord
*nsec3
, const char *domain
, const char *zone
, char **ret
) {
1417 uint8_t hashed
[DNSSEC_HASH_SIZE_MAX
];
1425 hashed_size
= dnssec_nsec3_hash(nsec3
, domain
, hashed
);
1426 if (hashed_size
< 0)
1429 return nsec3_hashed_domain_format(hashed
, (size_t) hashed_size
, zone
, ret
);
1432 /* See RFC 5155, Section 8
1433 * First try to find a NSEC3 record that matches our query precisely, if that fails, find the closest
1434 * enclosure. Secondly, find a proof that there is no closer enclosure and either a proof that there
1435 * is no wildcard domain as a direct descendant of the closest enclosure, or find an NSEC3 record that
1436 * matches the wildcard domain.
1438 * Based on this we can prove either the existence of the record in @key, or NXDOMAIN or NODATA, or
1439 * that there is no proof either way. The latter is the case if a the proof of non-existence of a given
1440 * name uses an NSEC3 record with the opt-out bit set. Lastly, if we are given insufficient NSEC3 records
1441 * to conclude anything we indicate this by returning NO_RR. */
1442 static int dnssec_test_nsec3(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1443 _cleanup_free_
char *next_closer_domain
= NULL
, *wildcard_domain
= NULL
;
1444 const char *zone
, *p
, *pp
= NULL
, *wildcard
;
1445 DnsResourceRecord
*rr
, *enclosure_rr
, *zone_rr
, *wildcard_rr
= NULL
;
1446 DnsAnswerFlags flags
;
1448 bool a
, no_closer
= false, no_wildcard
= false, optout
= false;
1453 /* First step, find the zone name and the NSEC3 parameters of the zone.
1454 * it is sufficient to look for the longest common suffix we find with
1455 * any NSEC3 RR in the response. Any NSEC3 record will do as all NSEC3
1456 * records from a given zone in a response must use the same
1458 zone
= dns_resource_key_name(key
);
1460 DNS_ANSWER_FOREACH_FLAGS(zone_rr
, flags
, answer
) {
1461 r
= nsec3_is_good(zone_rr
, NULL
);
1467 r
= dns_name_equal_skip(dns_resource_key_name(zone_rr
->key
), 1, zone
);
1474 /* Strip one label from the front */
1475 r
= dns_name_parent(&zone
);
1482 *result
= DNSSEC_NSEC_NO_RR
;
1486 /* Second step, find the closest encloser NSEC3 RR in 'answer' that matches 'key' */
1487 p
= dns_resource_key_name(key
);
1489 _cleanup_free_
char *hashed_domain
= NULL
;
1491 hashed_size
= nsec3_hashed_domain_make(zone_rr
, p
, zone
, &hashed_domain
);
1492 if (hashed_size
== -EOPNOTSUPP
) {
1493 *result
= DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
;
1496 if (hashed_size
< 0)
1499 DNS_ANSWER_FOREACH_FLAGS(enclosure_rr
, flags
, answer
) {
1501 r
= nsec3_is_good(enclosure_rr
, zone_rr
);
1507 if (enclosure_rr
->nsec3
.next_hashed_name_size
!= (size_t) hashed_size
)
1510 r
= dns_name_equal(dns_resource_key_name(enclosure_rr
->key
), hashed_domain
);
1514 a
= flags
& DNS_ANSWER_AUTHENTICATED
;
1515 goto found_closest_encloser
;
1519 /* We didn't find the closest encloser with this name,
1520 * but let's remember this domain name, it might be
1521 * the next closer name */
1525 /* Strip one label from the front */
1526 r
= dns_name_parent(&p
);
1533 *result
= DNSSEC_NSEC_NO_RR
;
1536 found_closest_encloser
:
1537 /* We found a closest encloser in 'p'; next closer is 'pp' */
1540 /* We have an exact match! If we area looking for a DS RR, then we must insist that we got the NSEC3 RR
1541 * from the parent. Otherwise the one from the child. Do so, by checking whether SOA and NS are
1542 * appropriately set. */
1544 if (key
->type
== DNS_TYPE_DS
) {
1545 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1548 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1549 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1553 /* No next closer NSEC3 RR. That means there's a direct NSEC3 RR for our key. */
1554 if (bitmap_isset(enclosure_rr
->nsec3
.types
, key
->type
))
1555 *result
= DNSSEC_NSEC_FOUND
;
1556 else if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1557 *result
= DNSSEC_NSEC_CNAME
;
1559 *result
= DNSSEC_NSEC_NODATA
;
1564 *ttl
= enclosure_rr
->ttl
;
1569 /* Ensure this is not a DNAME domain, see RFC5155, section 8.3. */
1570 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_DNAME
))
1573 /* Ensure that this data is from the delegated domain
1574 * (i.e. originates from the "lower" DNS server), and isn't
1575 * just glue records (i.e. doesn't originate from the "upper"
1577 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1578 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1581 /* Prove that there is no next closer and whether or not there is a wildcard domain. */
1583 wildcard
= strjoina("*.", p
);
1584 r
= nsec3_hashed_domain_make(enclosure_rr
, wildcard
, zone
, &wildcard_domain
);
1587 if (r
!= hashed_size
)
1590 r
= nsec3_hashed_domain_make(enclosure_rr
, pp
, zone
, &next_closer_domain
);
1593 if (r
!= hashed_size
)
1596 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1597 _cleanup_free_
char *next_hashed_domain
= NULL
;
1599 r
= nsec3_is_good(rr
, zone_rr
);
1605 r
= nsec3_hashed_domain_format(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, zone
, &next_hashed_domain
);
1609 r
= dns_name_between(dns_resource_key_name(rr
->key
), next_closer_domain
, next_hashed_domain
);
1613 if (rr
->nsec3
.flags
& 1)
1616 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1621 r
= dns_name_equal(dns_resource_key_name(rr
->key
), wildcard_domain
);
1625 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1630 r
= dns_name_between(dns_resource_key_name(rr
->key
), wildcard_domain
, next_hashed_domain
);
1634 if (rr
->nsec3
.flags
& 1)
1635 /* This only makes sense if we have a wildcard delegation, which is
1636 * very unlikely, see RFC 4592, Section 4.2, but we cannot rely on
1637 * this not happening, so hence cannot simply conclude NXDOMAIN as
1641 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1647 if (wildcard_rr
&& no_wildcard
)
1651 *result
= DNSSEC_NSEC_NO_RR
;
1656 /* A wildcard exists that matches our query. */
1658 /* This is not specified in any RFC to the best of my knowledge, but
1659 * if the next closer enclosure is covered by an opt-out NSEC3 RR
1660 * it means that we cannot prove that the source of synthesis is
1661 * correct, as there may be a closer match. */
1662 *result
= DNSSEC_NSEC_OPTOUT
;
1663 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, key
->type
))
1664 *result
= DNSSEC_NSEC_FOUND
;
1665 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1666 *result
= DNSSEC_NSEC_CNAME
;
1668 *result
= DNSSEC_NSEC_NODATA
;
1671 /* The RFC only specifies that we have to care for optout for NODATA for
1672 * DS records. However, children of an insecure opt-out delegation should
1673 * also be considered opt-out, rather than verified NXDOMAIN.
1674 * Note that we do not require a proof of wildcard non-existence if the
1675 * next closer domain is covered by an opt-out, as that would not provide
1676 * any additional information. */
1677 *result
= DNSSEC_NSEC_OPTOUT
;
1678 else if (no_wildcard
)
1679 *result
= DNSSEC_NSEC_NXDOMAIN
;
1681 *result
= DNSSEC_NSEC_NO_RR
;
1691 *ttl
= enclosure_rr
->ttl
;
1696 static int dnssec_nsec_wildcard_equal(DnsResourceRecord
*rr
, const char *name
) {
1697 char label
[DNS_LABEL_MAX
];
1702 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1704 /* Checks whether the specified RR has a name beginning in "*.", and if the rest is a suffix of our name */
1706 if (rr
->n_skip_labels_source
!= 1)
1709 n
= dns_resource_key_name(rr
->key
);
1710 r
= dns_label_unescape(&n
, label
, sizeof label
, 0);
1713 if (r
!= 1 || label
[0] != '*')
1716 return dns_name_endswith(name
, n
);
1719 static int dnssec_nsec_in_path(DnsResourceRecord
*rr
, const char *name
) {
1720 const char *nn
, *common_suffix
;
1724 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1726 /* Checks whether the specified nsec RR indicates that name is an empty non-terminal (ENT)
1728 * A couple of examples:
1730 * NSEC bar → waldo.foo.bar: indicates that foo.bar exists and is an ENT
1731 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that xoo.bar and zzz.xoo.bar exist and are ENTs
1732 * NSEC yyy.zzz.xoo.bar → bar: indicates pretty much nothing about ENTs
1735 /* First, determine parent of next domain. */
1736 nn
= rr
->nsec
.next_domain_name
;
1737 r
= dns_name_parent(&nn
);
1741 /* If the name we just determined is not equal or child of the name we are interested in, then we can't say
1742 * anything at all. */
1743 r
= dns_name_endswith(nn
, name
);
1747 /* 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. */
1748 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
1752 return dns_name_endswith(name
, common_suffix
);
1755 static int dnssec_nsec_from_parent_zone(DnsResourceRecord
*rr
, const char *name
) {
1759 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1761 /* Checks whether this NSEC originates to the parent zone or the child zone. */
1763 r
= dns_name_parent(&name
);
1767 r
= dns_name_equal(name
, dns_resource_key_name(rr
->key
));
1771 /* DNAME, and NS without SOA is an indication for a delegation. */
1772 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_DNAME
))
1775 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) && !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1781 static int dnssec_nsec_covers(DnsResourceRecord
*rr
, const char *name
) {
1786 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1788 /* Checks whether the name is covered by this NSEC RR. This means, that the name is somewhere below the NSEC's
1789 * signer name, and between the NSEC's two names. */
1791 r
= dns_resource_record_signer(rr
, &signer
);
1795 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
1799 return dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
1802 static int dnssec_nsec_generate_wildcard(DnsResourceRecord
*rr
, const char *name
, char **wc
) {
1803 const char *common_suffix1
, *common_suffix2
, *signer
;
1804 int r
, labels1
, labels2
;
1807 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1809 /* Generates "Wildcard at the Closest Encloser" for the given name and NSEC RR. */
1811 r
= dns_resource_record_signer(rr
, &signer
);
1815 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
1819 r
= dns_name_common_suffix(name
, dns_resource_key_name(rr
->key
), &common_suffix1
);
1823 r
= dns_name_common_suffix(name
, rr
->nsec
.next_domain_name
, &common_suffix2
);
1827 labels1
= dns_name_count_labels(common_suffix1
);
1831 labels2
= dns_name_count_labels(common_suffix2
);
1835 if (labels1
> labels2
)
1836 r
= dns_name_concat("*", common_suffix1
, 0, wc
);
1838 r
= dns_name_concat("*", common_suffix2
, 0, wc
);
1846 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1847 bool have_nsec3
= false, covering_rr_authenticated
= false, wildcard_rr_authenticated
= false;
1848 DnsResourceRecord
*rr
, *covering_rr
= NULL
, *wildcard_rr
= NULL
;
1849 DnsAnswerFlags flags
;
1856 /* Look for any NSEC/NSEC3 RRs that say something about the specified key. */
1858 name
= dns_resource_key_name(key
);
1860 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1862 if (rr
->key
->class != key
->class)
1865 have_nsec3
= have_nsec3
|| (rr
->key
->type
== DNS_TYPE_NSEC3
);
1867 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
1870 /* The following checks only make sense for NSEC RRs that are not expanded from a wildcard */
1871 r
= dns_resource_record_is_synthetic(rr
);
1877 /* Check if this is a direct match. If so, we have encountered a NODATA case */
1878 r
= dns_name_equal(dns_resource_key_name(rr
->key
), name
);
1882 /* If it's not a direct match, maybe it's a wild card match? */
1883 r
= dnssec_nsec_wildcard_equal(rr
, name
);
1888 if (key
->type
== DNS_TYPE_DS
) {
1889 /* If we look for a DS RR and the server sent us the NSEC RR of the child zone
1890 * we have a problem. For DS RRs we want the NSEC RR from the parent */
1891 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1894 /* For all RR types, ensure that if NS is set SOA is set too, so that we know
1895 * we got the child's NSEC. */
1896 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) &&
1897 !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1901 if (bitmap_isset(rr
->nsec
.types
, key
->type
))
1902 *result
= DNSSEC_NSEC_FOUND
;
1903 else if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_CNAME
))
1904 *result
= DNSSEC_NSEC_CNAME
;
1906 *result
= DNSSEC_NSEC_NODATA
;
1909 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1916 /* Check if the name we are looking for is an empty non-terminal within the owner or next name
1917 * of the NSEC RR. */
1918 r
= dnssec_nsec_in_path(rr
, name
);
1922 *result
= DNSSEC_NSEC_NODATA
;
1925 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1932 /* The following two "covering" checks, are not useful if the NSEC is from the parent */
1933 r
= dnssec_nsec_from_parent_zone(rr
, name
);
1939 /* Check if this NSEC RR proves the absence of an explicit RR under this name */
1940 r
= dnssec_nsec_covers(rr
, name
);
1943 if (r
> 0 && (!covering_rr
|| !covering_rr_authenticated
)) {
1945 covering_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1950 _cleanup_free_
char *wc
= NULL
;
1951 r
= dnssec_nsec_generate_wildcard(covering_rr
, name
, &wc
);
1955 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1957 if (rr
->key
->class != key
->class)
1960 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
1963 /* Check if this NSEC RR proves the nonexistence of the wildcard */
1964 r
= dnssec_nsec_covers(rr
, wc
);
1967 if (r
> 0 && (!wildcard_rr
|| !wildcard_rr_authenticated
)) {
1969 wildcard_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1974 if (covering_rr
&& wildcard_rr
) {
1975 /* If we could prove that neither the name itself, nor the wildcard at the closest encloser exists, we
1976 * proved the NXDOMAIN case. */
1977 *result
= DNSSEC_NSEC_NXDOMAIN
;
1980 *authenticated
= covering_rr_authenticated
&& wildcard_rr_authenticated
;
1982 *ttl
= MIN(covering_rr
->ttl
, wildcard_rr
->ttl
);
1987 /* OK, this was not sufficient. Let's see if NSEC3 can help. */
1989 return dnssec_test_nsec3(answer
, key
, result
, authenticated
, ttl
);
1991 /* No approproate NSEC RR found, report this. */
1992 *result
= DNSSEC_NSEC_NO_RR
;
1996 static int dnssec_nsec_test_enclosed(DnsAnswer
*answer
, uint16_t type
, const char *name
, const char *zone
, bool *authenticated
) {
1997 DnsResourceRecord
*rr
;
1998 DnsAnswerFlags flags
;
2004 /* Checks whether there's an NSEC/NSEC3 that proves that the specified 'name' is non-existing in the specified
2005 * 'zone'. The 'zone' must be a suffix of the 'name'. */
2007 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
2010 if (rr
->key
->type
!= type
&& type
!= DNS_TYPE_ANY
)
2013 switch (rr
->key
->type
) {
2017 /* We only care for NSEC RRs from the indicated zone */
2018 r
= dns_resource_record_is_signer(rr
, zone
);
2024 r
= dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
2031 case DNS_TYPE_NSEC3
: {
2032 _cleanup_free_
char *hashed_domain
= NULL
, *next_hashed_domain
= NULL
;
2034 /* We only care for NSEC3 RRs from the indicated zone */
2035 r
= dns_resource_record_is_signer(rr
, zone
);
2041 r
= nsec3_is_good(rr
, NULL
);
2047 /* Format the domain we are testing with the NSEC3 RR's hash function */
2048 r
= nsec3_hashed_domain_make(
2055 if ((size_t) r
!= rr
->nsec3
.next_hashed_name_size
)
2058 /* Format the NSEC3's next hashed name as proper domain name */
2059 r
= nsec3_hashed_domain_format(
2060 rr
->nsec3
.next_hashed_name
,
2061 rr
->nsec3
.next_hashed_name_size
,
2063 &next_hashed_domain
);
2067 r
= dns_name_between(dns_resource_key_name(rr
->key
), hashed_domain
, next_hashed_domain
);
2081 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2089 static int dnssec_test_positive_wildcard_nsec3(
2094 bool *authenticated
) {
2096 const char *next_closer
= NULL
;
2099 /* Run a positive NSEC3 wildcard proof. Specifically:
2101 * A proof that the "next closer" of the generating wildcard does not exist.
2103 * Note a key difference between the NSEC3 and NSEC versions of the proof. NSEC RRs don't have to exist for
2104 * empty non-transients. NSEC3 RRs however have to. This means it's sufficient to check if the next closer name
2105 * exists for the NSEC3 RR and we are done.
2107 * 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
2108 * c.d.e.f does not exist. */
2112 r
= dns_name_parent(&name
);
2116 r
= dns_name_equal(name
, source
);
2123 return dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC3
, next_closer
, zone
, authenticated
);
2126 static int dnssec_test_positive_wildcard_nsec(
2131 bool *_authenticated
) {
2133 bool authenticated
= true;
2136 /* Run a positive NSEC wildcard proof. Specifically:
2138 * A proof that there's neither a wildcard name nor a non-wildcard name that is a suffix of the name "name" and
2139 * a prefix of the synthesizing source "source" in the zone "zone".
2141 * See RFC 5155, Section 8.8 and RFC 4035, Section 5.3.4
2143 * 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
2144 * have to prove that none of the following exist:
2154 _cleanup_free_
char *wc
= NULL
;
2157 /* Check if there's an NSEC or NSEC3 RR that proves that the mame we determined is really non-existing,
2158 * i.e between the owner name and the next name of an NSEC RR. */
2159 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, name
, zone
, &a
);
2163 authenticated
= authenticated
&& a
;
2165 /* Strip one label off */
2166 r
= dns_name_parent(&name
);
2170 /* Did we reach the source of synthesis? */
2171 r
= dns_name_equal(name
, source
);
2175 /* Successful exit */
2176 *_authenticated
= authenticated
;
2180 /* Safety check, that the source of synthesis is still our suffix */
2181 r
= dns_name_endswith(name
, source
);
2187 /* Replace the label we stripped off with an asterisk */
2188 wc
= strappend("*.", name
);
2192 /* And check if the proof holds for the asterisk name, too */
2193 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, wc
, zone
, &a
);
2197 authenticated
= authenticated
&& a
;
2198 /* In the next iteration we'll check the non-asterisk-prefixed version */
2202 int dnssec_test_positive_wildcard(
2207 bool *authenticated
) {
2214 assert(authenticated
);
2216 r
= dns_answer_contains_zone_nsec3(answer
, zone
);
2220 return dnssec_test_positive_wildcard_nsec3(answer
, name
, source
, zone
, authenticated
);
2222 return dnssec_test_positive_wildcard_nsec(answer
, name
, source
, zone
, authenticated
);
2227 int dnssec_verify_rrset(
2229 const DnsResourceKey
*key
,
2230 DnsResourceRecord
*rrsig
,
2231 DnsResourceRecord
*dnskey
,
2233 DnssecResult
*result
) {
2238 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
2243 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
2248 int dnssec_verify_rrset_search(
2250 const DnsResourceKey
*key
,
2251 DnsAnswer
*validated_dnskeys
,
2253 DnssecResult
*result
,
2254 DnsResourceRecord
**ret_rrsig
) {
2259 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
2264 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
2269 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
2274 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
2279 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
2284 int dnssec_test_positive_wildcard(
2289 bool *authenticated
) {
2296 static const char* const dnssec_result_table
[_DNSSEC_RESULT_MAX
] = {
2297 [DNSSEC_VALIDATED
] = "validated",
2298 [DNSSEC_VALIDATED_WILDCARD
] = "validated-wildcard",
2299 [DNSSEC_INVALID
] = "invalid",
2300 [DNSSEC_SIGNATURE_EXPIRED
] = "signature-expired",
2301 [DNSSEC_UNSUPPORTED_ALGORITHM
] = "unsupported-algorithm",
2302 [DNSSEC_NO_SIGNATURE
] = "no-signature",
2303 [DNSSEC_MISSING_KEY
] = "missing-key",
2304 [DNSSEC_UNSIGNED
] = "unsigned",
2305 [DNSSEC_FAILED_AUXILIARY
] = "failed-auxiliary",
2306 [DNSSEC_NSEC_MISMATCH
] = "nsec-mismatch",
2307 [DNSSEC_INCOMPATIBLE_SERVER
] = "incompatible-server",
2309 DEFINE_STRING_TABLE_LOOKUP(dnssec_result
, DnssecResult
);
2311 static const char* const dnssec_verdict_table
[_DNSSEC_VERDICT_MAX
] = {
2312 [DNSSEC_SECURE
] = "secure",
2313 [DNSSEC_INSECURE
] = "insecure",
2314 [DNSSEC_BOGUS
] = "bogus",
2315 [DNSSEC_INDETERMINATE
] = "indeterminate",
2317 DEFINE_STRING_TABLE_LOOKUP(dnssec_verdict
, DnssecVerdict
);