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 "resolved-dns-dnssec.h"
16 #include "resolved-dns-packet.h"
17 #include "string-table.h"
19 #define VERIFY_RRS_MAX 256
20 #define MAX_KEY_SIZE (32*1024)
22 /* Permit a maximum clock skew of 1h 10min. This should be enough to deal with DST confusion */
23 #define SKEW_MAX (1*USEC_PER_HOUR + 10*USEC_PER_MINUTE)
25 /* Maximum number of NSEC3 iterations we'll do. RFC5155 says 2500 shall be the maximum useful value */
26 #define NSEC3_ITERATIONS_MAX 2500
29 * The DNSSEC Chain of trust:
31 * Normal RRs are protected via RRSIG RRs in combination with DNSKEY RRs, all in the same zone
32 * DNSKEY RRs are either protected like normal RRs, or via a DS from a zone "higher" up the tree
33 * DS RRs are protected like normal RRs
36 * Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS
39 uint16_t dnssec_keytag(DnsResourceRecord
*dnskey
, bool mask_revoke
) {
44 /* The algorithm from RFC 4034, Appendix B. */
47 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
49 f
= (uint32_t) dnskey
->dnskey
.flags
;
52 f
&= ~DNSKEY_FLAG_REVOKE
;
54 sum
= f
+ ((((uint32_t) dnskey
->dnskey
.protocol
) << 8) + (uint32_t) dnskey
->dnskey
.algorithm
);
56 p
= dnskey
->dnskey
.key
;
58 for (i
= 0; i
< dnskey
->dnskey
.key_size
; i
++)
59 sum
+= (i
& 1) == 0 ? (uint32_t) p
[i
] << 8 : (uint32_t) p
[i
];
61 sum
+= (sum
>> 16) & UINT32_C(0xFFFF);
63 return sum
& UINT32_C(0xFFFF);
66 int dnssec_canonicalize(const char *n
, char *buffer
, size_t buffer_max
) {
70 /* Converts the specified hostname into DNSSEC canonicalized
77 r
= dns_label_unescape(&n
, buffer
, buffer_max
);
83 if (buffer_max
< (size_t) r
+ 2)
86 /* The DNSSEC canonical form is not clear on what to
87 * do with dots appearing in labels, the way DNS-SD
88 * does it. Refuse it for now. */
90 if (memchr(buffer
, '.', r
))
93 ascii_strlower_n(buffer
, (size_t) r
);
103 /* Not even a single label: this is the root domain name */
105 assert(buffer_max
> 2);
117 static int rr_compare(const void *a
, const void *b
) {
118 DnsResourceRecord
**x
= (DnsResourceRecord
**) a
, **y
= (DnsResourceRecord
**) b
;
122 /* Let's order the RRs according to RFC 4034, Section 6.3 */
126 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 if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x
) < DNS_RESOURCE_RECORD_RDATA_SIZE(*y
))
139 else if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x
) > DNS_RESOURCE_RECORD_RDATA_SIZE(*y
))
145 static int dnssec_rsa_verify_raw(
146 const char *hash_algorithm
,
147 const void *signature
, size_t signature_size
,
148 const void *data
, size_t data_size
,
149 const void *exponent
, size_t exponent_size
,
150 const void *modulus
, size_t modulus_size
) {
152 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
153 gcry_mpi_t n
= NULL
, e
= NULL
, s
= NULL
;
157 assert(hash_algorithm
);
159 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature
, signature_size
, NULL
);
165 ge
= gcry_mpi_scan(&e
, GCRYMPI_FMT_USG
, exponent
, exponent_size
, NULL
);
171 ge
= gcry_mpi_scan(&n
, GCRYMPI_FMT_USG
, modulus
, modulus_size
, NULL
);
177 ge
= gcry_sexp_build(&signature_sexp
,
179 "(sig-val (rsa (s %m)))",
187 ge
= gcry_sexp_build(&data_sexp
,
189 "(data (flags pkcs1) (hash %s %b))",
198 ge
= gcry_sexp_build(&public_key_sexp
,
200 "(public-key (rsa (n %m) (e %m)))",
208 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
209 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
212 log_debug("RSA signature check failed: %s", gpg_strerror(ge
));
226 gcry_sexp_release(public_key_sexp
);
228 gcry_sexp_release(signature_sexp
);
230 gcry_sexp_release(data_sexp
);
235 static int dnssec_rsa_verify(
236 const char *hash_algorithm
,
237 const void *hash
, size_t hash_size
,
238 DnsResourceRecord
*rrsig
,
239 DnsResourceRecord
*dnskey
) {
241 size_t exponent_size
, modulus_size
;
242 void *exponent
, *modulus
;
244 assert(hash_algorithm
);
246 assert(hash_size
> 0);
250 if (*(uint8_t*) dnskey
->dnskey
.key
== 0) {
251 /* exponent is > 255 bytes long */
253 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 3;
255 ((size_t) (((uint8_t*) dnskey
->dnskey
.key
)[1]) << 8) |
256 ((size_t) ((uint8_t*) dnskey
->dnskey
.key
)[2]);
258 if (exponent_size
< 256)
261 if (3 + exponent_size
>= dnskey
->dnskey
.key_size
)
264 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 3 + exponent_size
;
265 modulus_size
= dnskey
->dnskey
.key_size
- 3 - exponent_size
;
268 /* exponent is <= 255 bytes long */
270 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 1;
271 exponent_size
= (size_t) ((uint8_t*) dnskey
->dnskey
.key
)[0];
273 if (exponent_size
<= 0)
276 if (1 + exponent_size
>= dnskey
->dnskey
.key_size
)
279 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 1 + exponent_size
;
280 modulus_size
= dnskey
->dnskey
.key_size
- 1 - exponent_size
;
283 return dnssec_rsa_verify_raw(
285 rrsig
->rrsig
.signature
, rrsig
->rrsig
.signature_size
,
287 exponent
, exponent_size
,
288 modulus
, modulus_size
);
291 static int dnssec_ecdsa_verify_raw(
292 const char *hash_algorithm
,
294 const void *signature_r
, size_t signature_r_size
,
295 const void *signature_s
, size_t signature_s_size
,
296 const void *data
, size_t data_size
,
297 const void *key
, size_t key_size
) {
299 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
300 gcry_mpi_t q
= NULL
, r
= NULL
, s
= NULL
;
304 assert(hash_algorithm
);
306 ge
= gcry_mpi_scan(&r
, GCRYMPI_FMT_USG
, signature_r
, signature_r_size
, NULL
);
312 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature_s
, signature_s_size
, NULL
);
318 ge
= gcry_mpi_scan(&q
, GCRYMPI_FMT_USG
, key
, key_size
, NULL
);
324 ge
= gcry_sexp_build(&signature_sexp
,
326 "(sig-val (ecdsa (r %m) (s %m)))",
334 ge
= gcry_sexp_build(&data_sexp
,
336 "(data (flags rfc6979) (hash %s %b))",
345 ge
= gcry_sexp_build(&public_key_sexp
,
347 "(public-key (ecc (curve %s) (q %m)))",
355 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
356 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
359 log_debug("ECDSA signature check failed: %s", gpg_strerror(ge
));
372 gcry_sexp_release(public_key_sexp
);
374 gcry_sexp_release(signature_sexp
);
376 gcry_sexp_release(data_sexp
);
381 static int dnssec_ecdsa_verify(
382 const char *hash_algorithm
,
384 const void *hash
, size_t hash_size
,
385 DnsResourceRecord
*rrsig
,
386 DnsResourceRecord
*dnskey
) {
397 if (algorithm
== DNSSEC_ALGORITHM_ECDSAP256SHA256
) {
399 curve
= "NIST P-256";
400 } else if (algorithm
== DNSSEC_ALGORITHM_ECDSAP384SHA384
) {
402 curve
= "NIST P-384";
406 if (dnskey
->dnskey
.key_size
!= key_size
* 2)
409 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
412 q
= alloca(key_size
*2 + 1);
413 q
[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
414 memcpy(q
+1, dnskey
->dnskey
.key
, key_size
*2);
416 return dnssec_ecdsa_verify_raw(
419 rrsig
->rrsig
.signature
, key_size
,
420 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
425 #if GCRYPT_VERSION_NUMBER >= 0x010600
426 static int dnssec_eddsa_verify_raw(
428 const void *signature_r
, size_t signature_r_size
,
429 const void *signature_s
, size_t signature_s_size
,
430 const void *data
, size_t data_size
,
431 const void *key
, size_t key_size
) {
433 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
437 ge
= gcry_sexp_build(&signature_sexp
,
439 "(sig-val (eddsa (r %b) (s %b)))",
440 (int) signature_r_size
,
442 (int) signature_s_size
,
449 ge
= gcry_sexp_build(&data_sexp
,
451 "(data (flags eddsa) (hash-algo sha512) (value %b))",
459 ge
= gcry_sexp_build(&public_key_sexp
,
461 "(public-key (ecc (curve %s) (flags eddsa) (q %b)))",
470 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
471 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
474 log_debug("EdDSA signature check failed: %s", gpg_strerror(ge
));
480 gcry_sexp_release(public_key_sexp
);
482 gcry_sexp_release(signature_sexp
);
484 gcry_sexp_release(data_sexp
);
489 static int dnssec_eddsa_verify(
491 const void *data
, size_t data_size
,
492 DnsResourceRecord
*rrsig
,
493 DnsResourceRecord
*dnskey
) {
497 if (algorithm
== DNSSEC_ALGORITHM_ED25519
) {
503 if (dnskey
->dnskey
.key_size
!= key_size
)
506 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
509 return dnssec_eddsa_verify_raw(
511 rrsig
->rrsig
.signature
, key_size
,
512 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
514 dnskey
->dnskey
.key
, key_size
);
518 static void md_add_uint8(gcry_md_hd_t md
, uint8_t v
) {
519 gcry_md_write(md
, &v
, sizeof(v
));
522 static void md_add_uint16(gcry_md_hd_t md
, uint16_t v
) {
524 gcry_md_write(md
, &v
, sizeof(v
));
527 static void fwrite_uint8(FILE *fp
, uint8_t v
) {
528 fwrite(&v
, sizeof(v
), 1, fp
);
531 static void fwrite_uint16(FILE *fp
, uint16_t v
) {
533 fwrite(&v
, sizeof(v
), 1, fp
);
536 static void fwrite_uint32(FILE *fp
, uint32_t v
) {
538 fwrite(&v
, sizeof(v
), 1, fp
);
541 static int dnssec_rrsig_prepare(DnsResourceRecord
*rrsig
) {
542 int n_key_labels
, n_signer_labels
;
546 /* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source and
547 * .n_skip_labels_signer fields so that we can use them later on. */
550 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
552 /* Check if this RRSIG RR is already prepared */
553 if (rrsig
->n_skip_labels_source
!= (unsigned) -1)
556 if (rrsig
->rrsig
.inception
> rrsig
->rrsig
.expiration
)
559 name
= dns_resource_key_name(rrsig
->key
);
561 n_key_labels
= dns_name_count_labels(name
);
562 if (n_key_labels
< 0)
564 if (rrsig
->rrsig
.labels
> n_key_labels
)
567 n_signer_labels
= dns_name_count_labels(rrsig
->rrsig
.signer
);
568 if (n_signer_labels
< 0)
569 return n_signer_labels
;
570 if (n_signer_labels
> rrsig
->rrsig
.labels
)
573 r
= dns_name_skip(name
, n_key_labels
- n_signer_labels
, &name
);
579 /* Check if the signer is really a suffix of us */
580 r
= dns_name_equal(name
, rrsig
->rrsig
.signer
);
586 rrsig
->n_skip_labels_source
= n_key_labels
- rrsig
->rrsig
.labels
;
587 rrsig
->n_skip_labels_signer
= n_key_labels
- n_signer_labels
;
592 static int dnssec_rrsig_expired(DnsResourceRecord
*rrsig
, usec_t realtime
) {
593 usec_t expiration
, inception
, skew
;
596 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
598 if (realtime
== USEC_INFINITY
)
599 realtime
= now(CLOCK_REALTIME
);
601 expiration
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
602 inception
= rrsig
->rrsig
.inception
* USEC_PER_SEC
;
604 /* Consider inverted validity intervals as expired */
605 if (inception
> expiration
)
608 /* Permit a certain amount of clock skew of 10% of the valid
609 * time range. This takes inspiration from unbound's
611 skew
= (expiration
- inception
) / 10;
615 if (inception
< skew
)
620 if (expiration
+ skew
< expiration
)
621 expiration
= USEC_INFINITY
;
625 return realtime
< inception
|| realtime
> expiration
;
628 static int algorithm_to_gcrypt_md(uint8_t algorithm
) {
630 /* Translates a DNSSEC signature algorithm into a gcrypt
633 * Note that we implement all algorithms listed as "Must
634 * implement" and "Recommended to Implement" in RFC6944. We
635 * don't implement any algorithms that are listed as
636 * "Optional" or "Must Not Implement". Specifically, we do not
637 * implement RSAMD5, DSASHA1, DH, DSA-NSEC3-SHA1, and
642 case DNSSEC_ALGORITHM_RSASHA1
:
643 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
646 case DNSSEC_ALGORITHM_RSASHA256
:
647 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
648 return GCRY_MD_SHA256
;
650 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
651 return GCRY_MD_SHA384
;
653 case DNSSEC_ALGORITHM_RSASHA512
:
654 return GCRY_MD_SHA512
;
661 static void dnssec_fix_rrset_ttl(
662 DnsResourceRecord
*list
[],
664 DnsResourceRecord
*rrsig
,
673 for (k
= 0; k
< n
; k
++) {
674 DnsResourceRecord
*rr
= list
[k
];
676 /* Pick the TTL as the minimum of the RR's TTL, the
677 * RR's original TTL according to the RRSIG and the
678 * RRSIG's own TTL, see RFC 4035, Section 5.3.3 */
679 rr
->ttl
= MIN3(rr
->ttl
, rrsig
->rrsig
.original_ttl
, rrsig
->ttl
);
680 rr
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
682 /* Copy over information about the signer and wildcard source of synthesis */
683 rr
->n_skip_labels_source
= rrsig
->n_skip_labels_source
;
684 rr
->n_skip_labels_signer
= rrsig
->n_skip_labels_signer
;
687 rrsig
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
690 int dnssec_verify_rrset(
692 const DnsResourceKey
*key
,
693 DnsResourceRecord
*rrsig
,
694 DnsResourceRecord
*dnskey
,
696 DnssecResult
*result
) {
698 uint8_t wire_format_name
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
699 DnsResourceRecord
**list
, *rr
;
700 const char *source
, *name
;
701 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
705 _cleanup_free_
char *sig_data
= NULL
;
706 _cleanup_fclose_
FILE *f
= NULL
;
715 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
716 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
718 /* Verifies that the RRSet matches the specified "key" in "a",
719 * using the signature "rrsig" and the key "dnskey". It's
720 * assumed that RRSIG and DNSKEY match. */
722 r
= dnssec_rrsig_prepare(rrsig
);
724 *result
= DNSSEC_INVALID
;
730 r
= dnssec_rrsig_expired(rrsig
, realtime
);
734 *result
= DNSSEC_SIGNATURE_EXPIRED
;
738 name
= dns_resource_key_name(key
);
740 /* Some keys may only appear signed in the zone apex, and are invalid anywhere else. (SOA, NS...) */
741 if (dns_type_apex_only(rrsig
->rrsig
.type_covered
)) {
742 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
746 *result
= DNSSEC_INVALID
;
751 /* OTOH DS RRs may not appear in the zone apex, but are valid everywhere else. */
752 if (rrsig
->rrsig
.type_covered
== DNS_TYPE_DS
) {
753 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
757 *result
= DNSSEC_INVALID
;
762 /* Determine the "Source of Synthesis" and whether this is a wildcard RRSIG */
763 r
= dns_name_suffix(name
, rrsig
->rrsig
.labels
, &source
);
766 if (r
> 0 && !dns_type_may_wildcard(rrsig
->rrsig
.type_covered
)) {
767 /* We refuse to validate NSEC3 or SOA RRs that are synthesized from wildcards */
768 *result
= DNSSEC_INVALID
;
772 /* If we stripped a single label, then let's see if that maybe was "*". If so, we are not really
773 * synthesized from a wildcard, we are the wildcard itself. Treat that like a normal name. */
774 r
= dns_name_startswith(name
, "*");
784 /* Collect all relevant RRs in a single array, so that we can look at the RRset */
785 list
= newa(DnsResourceRecord
*, dns_answer_size(a
));
787 DNS_ANSWER_FOREACH(rr
, a
) {
788 r
= dns_resource_key_equal(key
, rr
->key
);
794 /* We need the wire format for ordering, and digest calculation */
795 r
= dns_resource_record_to_wire_format(rr
, true);
801 if (n
> VERIFY_RRS_MAX
)
808 /* Bring the RRs into canonical order */
809 qsort_safe(list
, n
, sizeof(DnsResourceRecord
*), rr_compare
);
811 f
= open_memstream(&sig_data
, &sig_size
);
814 __fsetlocking(f
, FSETLOCKING_BYCALLER
);
816 fwrite_uint16(f
, rrsig
->rrsig
.type_covered
);
817 fwrite_uint8(f
, rrsig
->rrsig
.algorithm
);
818 fwrite_uint8(f
, rrsig
->rrsig
.labels
);
819 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
820 fwrite_uint32(f
, rrsig
->rrsig
.expiration
);
821 fwrite_uint32(f
, rrsig
->rrsig
.inception
);
822 fwrite_uint16(f
, rrsig
->rrsig
.key_tag
);
824 r
= dns_name_to_wire_format(rrsig
->rrsig
.signer
, wire_format_name
, sizeof(wire_format_name
), true);
827 fwrite(wire_format_name
, 1, r
, f
);
829 /* Convert the source of synthesis into wire format */
830 r
= dns_name_to_wire_format(source
, wire_format_name
, sizeof(wire_format_name
), true);
834 for (k
= 0; k
< n
; k
++) {
839 /* Hash the source of synthesis. If this is a wildcard, then prefix it with the *. label */
841 fwrite((uint8_t[]) { 1, '*'}, sizeof(uint8_t), 2, f
);
842 fwrite(wire_format_name
, 1, r
, f
);
844 fwrite_uint16(f
, rr
->key
->type
);
845 fwrite_uint16(f
, rr
->key
->class);
846 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
848 l
= DNS_RESOURCE_RECORD_RDATA_SIZE(rr
);
851 fwrite_uint16(f
, (uint16_t) l
);
852 fwrite(DNS_RESOURCE_RECORD_RDATA(rr
), 1, l
, f
);
855 r
= fflush_and_check(f
);
859 initialize_libgcrypt(false);
861 switch (rrsig
->rrsig
.algorithm
) {
862 #if GCRYPT_VERSION_NUMBER >= 0x010600
863 case DNSSEC_ALGORITHM_ED25519
:
866 case DNSSEC_ALGORITHM_ED25519
:
868 case DNSSEC_ALGORITHM_ED448
:
869 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
872 /* OK, the RRs are now in canonical order. Let's calculate the digest */
873 md_algorithm
= algorithm_to_gcrypt_md(rrsig
->rrsig
.algorithm
);
874 if (md_algorithm
== -EOPNOTSUPP
) {
875 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
878 if (md_algorithm
< 0)
881 gcry_md_open(&md
, md_algorithm
, 0);
885 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
886 assert(hash_size
> 0);
888 gcry_md_write(md
, sig_data
, sig_size
);
890 hash
= gcry_md_read(md
, 0);
895 switch (rrsig
->rrsig
.algorithm
) {
897 case DNSSEC_ALGORITHM_RSASHA1
:
898 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
899 case DNSSEC_ALGORITHM_RSASHA256
:
900 case DNSSEC_ALGORITHM_RSASHA512
:
901 r
= dnssec_rsa_verify(
902 gcry_md_algo_name(md_algorithm
),
908 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
909 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
910 r
= dnssec_ecdsa_verify(
911 gcry_md_algo_name(md_algorithm
),
912 rrsig
->rrsig
.algorithm
,
917 #if GCRYPT_VERSION_NUMBER >= 0x010600
918 case DNSSEC_ALGORITHM_ED25519
:
919 r
= dnssec_eddsa_verify(
920 rrsig
->rrsig
.algorithm
,
930 /* Now, fix the ttl, expiry, and remember the synthesizing source and the signer */
932 dnssec_fix_rrset_ttl(list
, n
, rrsig
, realtime
);
935 *result
= DNSSEC_INVALID
;
937 *result
= DNSSEC_VALIDATED_WILDCARD
;
939 *result
= DNSSEC_VALIDATED
;
944 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
949 /* Checks if the specified DNSKEY RR matches the key used for
950 * the signature in the specified RRSIG RR */
952 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
955 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
957 if (dnskey
->key
->class != rrsig
->key
->class)
959 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
961 if (!revoked_ok
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
963 if (dnskey
->dnskey
.protocol
!= 3)
965 if (dnskey
->dnskey
.algorithm
!= rrsig
->rrsig
.algorithm
)
968 if (dnssec_keytag(dnskey
, false) != rrsig
->rrsig
.key_tag
)
971 return dns_name_equal(dns_resource_key_name(dnskey
->key
), rrsig
->rrsig
.signer
);
974 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
978 /* Checks if the specified RRSIG RR protects the RRSet of the specified RR key. */
980 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
982 if (rrsig
->key
->class != key
->class)
984 if (rrsig
->rrsig
.type_covered
!= key
->type
)
987 return dns_name_equal(dns_resource_key_name(rrsig
->key
), dns_resource_key_name(key
));
990 int dnssec_verify_rrset_search(
992 const DnsResourceKey
*key
,
993 DnsAnswer
*validated_dnskeys
,
995 DnssecResult
*result
,
996 DnsResourceRecord
**ret_rrsig
) {
998 bool found_rrsig
= false, found_invalid
= false, found_expired_rrsig
= false, found_unsupported_algorithm
= false;
999 DnsResourceRecord
*rrsig
;
1005 /* Verifies all RRs from "a" that match the key "key" against DNSKEYs in "validated_dnskeys" */
1007 if (!a
|| a
->n_rrs
<= 0)
1010 /* Iterate through each RRSIG RR. */
1011 DNS_ANSWER_FOREACH(rrsig
, a
) {
1012 DnsResourceRecord
*dnskey
;
1013 DnsAnswerFlags flags
;
1015 /* Is this an RRSIG RR that applies to RRs matching our key? */
1016 r
= dnssec_key_match_rrsig(key
, rrsig
);
1024 /* Look for a matching key */
1025 DNS_ANSWER_FOREACH_FLAGS(dnskey
, flags
, validated_dnskeys
) {
1026 DnssecResult one_result
;
1028 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1031 /* Is this a DNSKEY RR that matches they key of our RRSIG? */
1032 r
= dnssec_rrsig_match_dnskey(rrsig
, dnskey
, false);
1038 /* Take the time here, if it isn't set yet, so
1039 * that we do all validations with the same
1041 if (realtime
== USEC_INFINITY
)
1042 realtime
= now(CLOCK_REALTIME
);
1044 /* Yay, we found a matching RRSIG with a matching
1045 * DNSKEY, awesome. Now let's verify all entries of
1046 * the RRSet against the RRSIG and DNSKEY
1049 r
= dnssec_verify_rrset(a
, key
, rrsig
, dnskey
, realtime
, &one_result
);
1053 switch (one_result
) {
1055 case DNSSEC_VALIDATED
:
1056 case DNSSEC_VALIDATED_WILDCARD
:
1057 /* Yay, the RR has been validated,
1058 * return immediately, but fix up the expiry */
1062 *result
= one_result
;
1065 case DNSSEC_INVALID
:
1066 /* If the signature is invalid, let's try another
1067 key and/or signature. After all they
1068 key_tags and stuff are not unique, and
1069 might be shared by multiple keys. */
1070 found_invalid
= true;
1073 case DNSSEC_UNSUPPORTED_ALGORITHM
:
1074 /* If the key algorithm is
1075 unsupported, try another
1076 RRSIG/DNSKEY pair, but remember we
1077 encountered this, so that we can
1078 return a proper error when we
1079 encounter nothing better. */
1080 found_unsupported_algorithm
= true;
1083 case DNSSEC_SIGNATURE_EXPIRED
:
1084 /* If the signature is expired, try
1085 another one, but remember it, so
1086 that we can return this */
1087 found_expired_rrsig
= true;
1091 assert_not_reached("Unexpected DNSSEC validation result");
1096 if (found_expired_rrsig
)
1097 *result
= DNSSEC_SIGNATURE_EXPIRED
;
1098 else if (found_unsupported_algorithm
)
1099 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
1100 else if (found_invalid
)
1101 *result
= DNSSEC_INVALID
;
1102 else if (found_rrsig
)
1103 *result
= DNSSEC_MISSING_KEY
;
1105 *result
= DNSSEC_NO_SIGNATURE
;
1113 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
1114 DnsResourceRecord
*rr
;
1117 /* Checks whether there's at least one RRSIG in 'a' that proctects RRs of the specified key */
1119 DNS_ANSWER_FOREACH(rr
, a
) {
1120 r
= dnssec_key_match_rrsig(key
, rr
);
1130 static int digest_to_gcrypt_md(uint8_t algorithm
) {
1132 /* Translates a DNSSEC digest algorithm into a gcrypt digest identifier */
1134 switch (algorithm
) {
1136 case DNSSEC_DIGEST_SHA1
:
1137 return GCRY_MD_SHA1
;
1139 case DNSSEC_DIGEST_SHA256
:
1140 return GCRY_MD_SHA256
;
1142 case DNSSEC_DIGEST_SHA384
:
1143 return GCRY_MD_SHA384
;
1150 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
1151 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1152 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
1154 int md_algorithm
, r
;
1160 /* Implements DNSKEY verification by a DS, according to RFC 4035, section 5.2 */
1162 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1164 if (ds
->key
->type
!= DNS_TYPE_DS
)
1166 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
1167 return -EKEYREJECTED
;
1168 if (!mask_revoke
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
1169 return -EKEYREJECTED
;
1170 if (dnskey
->dnskey
.protocol
!= 3)
1171 return -EKEYREJECTED
;
1173 if (dnskey
->dnskey
.algorithm
!= ds
->ds
.algorithm
)
1175 if (dnssec_keytag(dnskey
, mask_revoke
) != ds
->ds
.key_tag
)
1178 initialize_libgcrypt(false);
1180 md_algorithm
= digest_to_gcrypt_md(ds
->ds
.digest_type
);
1181 if (md_algorithm
< 0)
1182 return md_algorithm
;
1184 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
1185 assert(hash_size
> 0);
1187 if (ds
->ds
.digest_size
!= hash_size
)
1190 r
= dns_name_to_wire_format(dns_resource_key_name(dnskey
->key
), wire_format
, sizeof(wire_format
), true);
1194 gcry_md_open(&md
, md_algorithm
, 0);
1198 gcry_md_write(md
, wire_format
, r
);
1200 md_add_uint16(md
, dnskey
->dnskey
.flags
& ~DNSKEY_FLAG_REVOKE
);
1202 md_add_uint16(md
, dnskey
->dnskey
.flags
);
1203 md_add_uint8(md
, dnskey
->dnskey
.protocol
);
1204 md_add_uint8(md
, dnskey
->dnskey
.algorithm
);
1205 gcry_md_write(md
, dnskey
->dnskey
.key
, dnskey
->dnskey
.key_size
);
1207 result
= gcry_md_read(md
, 0);
1211 return memcmp(result
, ds
->ds
.digest
, ds
->ds
.digest_size
) == 0;
1214 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
1215 DnsResourceRecord
*ds
;
1216 DnsAnswerFlags flags
;
1221 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1224 DNS_ANSWER_FOREACH_FLAGS(ds
, flags
, validated_ds
) {
1226 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1229 if (ds
->key
->type
!= DNS_TYPE_DS
)
1231 if (ds
->key
->class != dnskey
->key
->class)
1234 r
= dns_name_equal(dns_resource_key_name(dnskey
->key
), dns_resource_key_name(ds
->key
));
1240 r
= dnssec_verify_dnskey_by_ds(dnskey
, ds
, false);
1241 if (IN_SET(r
, -EKEYREJECTED
, -EOPNOTSUPP
))
1242 return 0; /* The DNSKEY is revoked or otherwise invalid, or we don't support the digest algorithm */
1252 static int nsec3_hash_to_gcrypt_md(uint8_t algorithm
) {
1254 /* Translates a DNSSEC NSEC3 hash algorithm into a gcrypt digest identifier */
1256 switch (algorithm
) {
1258 case NSEC3_ALGORITHM_SHA1
:
1259 return GCRY_MD_SHA1
;
1266 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
1267 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1268 gcry_md_hd_t md
= NULL
;
1279 if (nsec3
->key
->type
!= DNS_TYPE_NSEC3
)
1282 if (nsec3
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
) {
1283 log_debug("Ignoring NSEC3 RR %s with excessive number of iterations.", dns_resource_record_to_string(nsec3
));
1287 algorithm
= nsec3_hash_to_gcrypt_md(nsec3
->nsec3
.algorithm
);
1291 initialize_libgcrypt(false);
1293 hash_size
= gcry_md_get_algo_dlen(algorithm
);
1294 assert(hash_size
> 0);
1296 if (nsec3
->nsec3
.next_hashed_name_size
!= hash_size
)
1299 r
= dns_name_to_wire_format(name
, wire_format
, sizeof(wire_format
), true);
1303 gcry_md_open(&md
, algorithm
, 0);
1307 gcry_md_write(md
, wire_format
, r
);
1308 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1310 result
= gcry_md_read(md
, 0);
1316 for (k
= 0; k
< nsec3
->nsec3
.iterations
; k
++) {
1317 uint8_t tmp
[hash_size
];
1318 memcpy(tmp
, result
, hash_size
);
1321 gcry_md_write(md
, tmp
, hash_size
);
1322 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1324 result
= gcry_md_read(md
, 0);
1331 memcpy(ret
, result
, hash_size
);
1332 r
= (int) hash_size
;
1339 static int nsec3_is_good(DnsResourceRecord
*rr
, DnsResourceRecord
*nsec3
) {
1345 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
1348 /* RFC 5155, Section 8.2 says we MUST ignore NSEC3 RRs with flags != 0 or 1 */
1349 if (!IN_SET(rr
->nsec3
.flags
, 0, 1))
1352 /* Ignore NSEC3 RRs whose algorithm we don't know */
1353 if (nsec3_hash_to_gcrypt_md(rr
->nsec3
.algorithm
) < 0)
1355 /* Ignore NSEC3 RRs with an excessive number of required iterations */
1356 if (rr
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1359 /* Ignore NSEC3 RRs generated from wildcards. If these NSEC3 RRs weren't correctly signed we can't make this
1360 * check (since rr->n_skip_labels_source is -1), but that's OK, as we won't trust them anyway in that case. */
1361 if (!IN_SET(rr
->n_skip_labels_source
, 0, (unsigned) -1))
1363 /* Ignore NSEC3 RRs that are located anywhere else than one label below the zone */
1364 if (!IN_SET(rr
->n_skip_labels_signer
, 1, (unsigned) -1))
1370 /* If a second NSEC3 RR is specified, also check if they are from the same zone. */
1372 if (nsec3
== rr
) /* Shortcut */
1375 if (rr
->key
->class != nsec3
->key
->class)
1377 if (rr
->nsec3
.algorithm
!= nsec3
->nsec3
.algorithm
)
1379 if (rr
->nsec3
.iterations
!= nsec3
->nsec3
.iterations
)
1381 if (rr
->nsec3
.salt_size
!= nsec3
->nsec3
.salt_size
)
1383 if (memcmp(rr
->nsec3
.salt
, nsec3
->nsec3
.salt
, rr
->nsec3
.salt_size
) != 0)
1386 a
= dns_resource_key_name(rr
->key
);
1387 r
= dns_name_parent(&a
); /* strip off hash */
1393 b
= dns_resource_key_name(nsec3
->key
);
1394 r
= dns_name_parent(&b
); /* strip off hash */
1400 /* Make sure both have the same parent */
1401 return dns_name_equal(a
, b
);
1404 static int nsec3_hashed_domain_format(const uint8_t *hashed
, size_t hashed_size
, const char *zone
, char **ret
) {
1405 _cleanup_free_
char *l
= NULL
;
1409 assert(hashed_size
> 0);
1413 l
= base32hexmem(hashed
, hashed_size
, false);
1417 j
= strjoin(l
, ".", zone
);
1422 return (int) hashed_size
;
1425 static int nsec3_hashed_domain_make(DnsResourceRecord
*nsec3
, const char *domain
, const char *zone
, char **ret
) {
1426 uint8_t hashed
[DNSSEC_HASH_SIZE_MAX
];
1434 hashed_size
= dnssec_nsec3_hash(nsec3
, domain
, hashed
);
1435 if (hashed_size
< 0)
1438 return nsec3_hashed_domain_format(hashed
, (size_t) hashed_size
, zone
, ret
);
1441 /* See RFC 5155, Section 8
1442 * First try to find a NSEC3 record that matches our query precisely, if that fails, find the closest
1443 * enclosure. Secondly, find a proof that there is no closer enclosure and either a proof that there
1444 * is no wildcard domain as a direct descendant of the closest enclosure, or find an NSEC3 record that
1445 * matches the wildcard domain.
1447 * Based on this we can prove either the existence of the record in @key, or NXDOMAIN or NODATA, or
1448 * that there is no proof either way. The latter is the case if a the proof of non-existence of a given
1449 * name uses an NSEC3 record with the opt-out bit set. Lastly, if we are given insufficient NSEC3 records
1450 * to conclude anything we indicate this by returning NO_RR. */
1451 static int dnssec_test_nsec3(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1452 _cleanup_free_
char *next_closer_domain
= NULL
, *wildcard_domain
= NULL
;
1453 const char *zone
, *p
, *pp
= NULL
, *wildcard
;
1454 DnsResourceRecord
*rr
, *enclosure_rr
, *zone_rr
, *wildcard_rr
= NULL
;
1455 DnsAnswerFlags flags
;
1457 bool a
, no_closer
= false, no_wildcard
= false, optout
= false;
1462 /* First step, find the zone name and the NSEC3 parameters of the zone.
1463 * it is sufficient to look for the longest common suffix we find with
1464 * any NSEC3 RR in the response. Any NSEC3 record will do as all NSEC3
1465 * records from a given zone in a response must use the same
1467 zone
= dns_resource_key_name(key
);
1469 DNS_ANSWER_FOREACH_FLAGS(zone_rr
, flags
, answer
) {
1470 r
= nsec3_is_good(zone_rr
, NULL
);
1476 r
= dns_name_equal_skip(dns_resource_key_name(zone_rr
->key
), 1, zone
);
1483 /* Strip one label from the front */
1484 r
= dns_name_parent(&zone
);
1491 *result
= DNSSEC_NSEC_NO_RR
;
1495 /* Second step, find the closest encloser NSEC3 RR in 'answer' that matches 'key' */
1496 p
= dns_resource_key_name(key
);
1498 _cleanup_free_
char *hashed_domain
= NULL
;
1500 hashed_size
= nsec3_hashed_domain_make(zone_rr
, p
, zone
, &hashed_domain
);
1501 if (hashed_size
== -EOPNOTSUPP
) {
1502 *result
= DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
;
1505 if (hashed_size
< 0)
1508 DNS_ANSWER_FOREACH_FLAGS(enclosure_rr
, flags
, answer
) {
1510 r
= nsec3_is_good(enclosure_rr
, zone_rr
);
1516 if (enclosure_rr
->nsec3
.next_hashed_name_size
!= (size_t) hashed_size
)
1519 r
= dns_name_equal(dns_resource_key_name(enclosure_rr
->key
), hashed_domain
);
1523 a
= flags
& DNS_ANSWER_AUTHENTICATED
;
1524 goto found_closest_encloser
;
1528 /* We didn't find the closest encloser with this name,
1529 * but let's remember this domain name, it might be
1530 * the next closer name */
1534 /* Strip one label from the front */
1535 r
= dns_name_parent(&p
);
1542 *result
= DNSSEC_NSEC_NO_RR
;
1545 found_closest_encloser
:
1546 /* We found a closest encloser in 'p'; next closer is 'pp' */
1549 /* We have an exact match! If we area looking for a DS RR, then we must insist that we got the NSEC3 RR
1550 * from the parent. Otherwise the one from the child. Do so, by checking whether SOA and NS are
1551 * appropriately set. */
1553 if (key
->type
== DNS_TYPE_DS
) {
1554 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1557 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1558 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1562 /* No next closer NSEC3 RR. That means there's a direct NSEC3 RR for our key. */
1563 if (bitmap_isset(enclosure_rr
->nsec3
.types
, key
->type
))
1564 *result
= DNSSEC_NSEC_FOUND
;
1565 else if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1566 *result
= DNSSEC_NSEC_CNAME
;
1568 *result
= DNSSEC_NSEC_NODATA
;
1573 *ttl
= enclosure_rr
->ttl
;
1578 /* Ensure this is not a DNAME domain, see RFC5155, section 8.3. */
1579 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_DNAME
))
1582 /* Ensure that this data is from the delegated domain
1583 * (i.e. originates from the "lower" DNS server), and isn't
1584 * just glue records (i.e. doesn't originate from the "upper"
1586 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1587 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1590 /* Prove that there is no next closer and whether or not there is a wildcard domain. */
1592 wildcard
= strjoina("*.", p
);
1593 r
= nsec3_hashed_domain_make(enclosure_rr
, wildcard
, zone
, &wildcard_domain
);
1596 if (r
!= hashed_size
)
1599 r
= nsec3_hashed_domain_make(enclosure_rr
, pp
, zone
, &next_closer_domain
);
1602 if (r
!= hashed_size
)
1605 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1606 _cleanup_free_
char *next_hashed_domain
= NULL
;
1608 r
= nsec3_is_good(rr
, zone_rr
);
1614 r
= nsec3_hashed_domain_format(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, zone
, &next_hashed_domain
);
1618 r
= dns_name_between(dns_resource_key_name(rr
->key
), next_closer_domain
, next_hashed_domain
);
1622 if (rr
->nsec3
.flags
& 1)
1625 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1630 r
= dns_name_equal(dns_resource_key_name(rr
->key
), wildcard_domain
);
1634 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1639 r
= dns_name_between(dns_resource_key_name(rr
->key
), wildcard_domain
, next_hashed_domain
);
1643 if (rr
->nsec3
.flags
& 1)
1644 /* This only makes sense if we have a wildcard delegation, which is
1645 * very unlikely, see RFC 4592, Section 4.2, but we cannot rely on
1646 * this not happening, so hence cannot simply conclude NXDOMAIN as
1650 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1656 if (wildcard_rr
&& no_wildcard
)
1660 *result
= DNSSEC_NSEC_NO_RR
;
1665 /* A wildcard exists that matches our query. */
1667 /* This is not specified in any RFC to the best of my knowledge, but
1668 * if the next closer enclosure is covered by an opt-out NSEC3 RR
1669 * it means that we cannot prove that the source of synthesis is
1670 * correct, as there may be a closer match. */
1671 *result
= DNSSEC_NSEC_OPTOUT
;
1672 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, key
->type
))
1673 *result
= DNSSEC_NSEC_FOUND
;
1674 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1675 *result
= DNSSEC_NSEC_CNAME
;
1677 *result
= DNSSEC_NSEC_NODATA
;
1680 /* The RFC only specifies that we have to care for optout for NODATA for
1681 * DS records. However, children of an insecure opt-out delegation should
1682 * also be considered opt-out, rather than verified NXDOMAIN.
1683 * Note that we do not require a proof of wildcard non-existence if the
1684 * next closer domain is covered by an opt-out, as that would not provide
1685 * any additional information. */
1686 *result
= DNSSEC_NSEC_OPTOUT
;
1687 else if (no_wildcard
)
1688 *result
= DNSSEC_NSEC_NXDOMAIN
;
1690 *result
= DNSSEC_NSEC_NO_RR
;
1700 *ttl
= enclosure_rr
->ttl
;
1705 static int dnssec_nsec_wildcard_equal(DnsResourceRecord
*rr
, const char *name
) {
1706 char label
[DNS_LABEL_MAX
];
1711 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1713 /* Checks whether the specified RR has a name beginning in "*.", and if the rest is a suffix of our name */
1715 if (rr
->n_skip_labels_source
!= 1)
1718 n
= dns_resource_key_name(rr
->key
);
1719 r
= dns_label_unescape(&n
, label
, sizeof(label
));
1722 if (r
!= 1 || label
[0] != '*')
1725 return dns_name_endswith(name
, n
);
1728 static int dnssec_nsec_in_path(DnsResourceRecord
*rr
, const char *name
) {
1729 const char *nn
, *common_suffix
;
1733 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1735 /* Checks whether the specified nsec RR indicates that name is an empty non-terminal (ENT)
1737 * A couple of examples:
1739 * NSEC bar → waldo.foo.bar: indicates that foo.bar exists and is an ENT
1740 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that xoo.bar and zzz.xoo.bar exist and are ENTs
1741 * NSEC yyy.zzz.xoo.bar → bar: indicates pretty much nothing about ENTs
1744 /* First, determine parent of next domain. */
1745 nn
= rr
->nsec
.next_domain_name
;
1746 r
= dns_name_parent(&nn
);
1750 /* If the name we just determined is not equal or child of the name we are interested in, then we can't say
1751 * anything at all. */
1752 r
= dns_name_endswith(nn
, name
);
1756 /* 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. */
1757 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
1761 return dns_name_endswith(name
, common_suffix
);
1764 static int dnssec_nsec_from_parent_zone(DnsResourceRecord
*rr
, const char *name
) {
1768 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1770 /* Checks whether this NSEC originates to the parent zone or the child zone. */
1772 r
= dns_name_parent(&name
);
1776 r
= dns_name_equal(name
, dns_resource_key_name(rr
->key
));
1780 /* DNAME, and NS without SOA is an indication for a delegation. */
1781 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_DNAME
))
1784 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) && !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1790 static int dnssec_nsec_covers(DnsResourceRecord
*rr
, const char *name
) {
1795 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1797 /* Checks whether the name is covered by this NSEC RR. This means, that the name is somewhere below the NSEC's
1798 * signer name, and between the NSEC's two names. */
1800 r
= dns_resource_record_signer(rr
, &signer
);
1804 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
1808 return dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
1811 static int dnssec_nsec_covers_wildcard(DnsResourceRecord
*rr
, const char *name
) {
1812 _cleanup_free_
char *wc
= NULL
;
1813 const char *common_suffix
, *signer
;
1817 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1819 /* Checks whether the "Wildcard at the Closest Encloser" is within the space covered by the specified
1820 * RR. Specifically, checks whether 'name' has the common suffix of the NSEC RR's owner and next names as
1821 * suffix, and whether the NSEC covers the name generated by that suffix prepended with an asterisk label.
1823 * NSEC bar → waldo.foo.bar: indicates that *.bar and *.foo.bar do not exist
1824 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that *.xoo.bar and *.zzz.xoo.bar do not exist (and more ...)
1825 * NSEC yyy.zzz.xoo.bar → bar: indicates that a number of wildcards don#t exist either...
1828 r
= dns_resource_record_signer(rr
, &signer
);
1832 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
1836 r
= dns_name_endswith(name
, dns_resource_key_name(rr
->key
));
1839 if (r
> 0) /* If the name we are interested in is a child of the NSEC RR, then append the asterisk to the NSEC
1841 r
= dns_name_concat("*", dns_resource_key_name(rr
->key
), &wc
);
1843 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
1847 r
= dns_name_concat("*", common_suffix
, &wc
);
1852 return dns_name_between(dns_resource_key_name(rr
->key
), wc
, rr
->nsec
.next_domain_name
);
1855 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1856 bool have_nsec3
= false, covering_rr_authenticated
= false, wildcard_rr_authenticated
= false;
1857 DnsResourceRecord
*rr
, *covering_rr
= NULL
, *wildcard_rr
= NULL
;
1858 DnsAnswerFlags flags
;
1865 /* Look for any NSEC/NSEC3 RRs that say something about the specified key. */
1867 name
= dns_resource_key_name(key
);
1869 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1871 if (rr
->key
->class != key
->class)
1874 have_nsec3
= have_nsec3
|| (rr
->key
->type
== DNS_TYPE_NSEC3
);
1876 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
1879 /* The following checks only make sense for NSEC RRs that are not expanded from a wildcard */
1880 r
= dns_resource_record_is_synthetic(rr
);
1886 /* Check if this is a direct match. If so, we have encountered a NODATA case */
1887 r
= dns_name_equal(dns_resource_key_name(rr
->key
), name
);
1891 /* If it's not a direct match, maybe it's a wild card match? */
1892 r
= dnssec_nsec_wildcard_equal(rr
, name
);
1897 if (key
->type
== DNS_TYPE_DS
) {
1898 /* If we look for a DS RR and the server sent us the NSEC RR of the child zone
1899 * we have a problem. For DS RRs we want the NSEC RR from the parent */
1900 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1903 /* For all RR types, ensure that if NS is set SOA is set too, so that we know
1904 * we got the child's NSEC. */
1905 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) &&
1906 !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1910 if (bitmap_isset(rr
->nsec
.types
, key
->type
))
1911 *result
= DNSSEC_NSEC_FOUND
;
1912 else if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_CNAME
))
1913 *result
= DNSSEC_NSEC_CNAME
;
1915 *result
= DNSSEC_NSEC_NODATA
;
1918 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1925 /* Check if the name we are looking for is an empty non-terminal within the owner or next name
1926 * of the NSEC RR. */
1927 r
= dnssec_nsec_in_path(rr
, name
);
1931 *result
= DNSSEC_NSEC_NODATA
;
1934 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1941 /* The following two "covering" checks, are not useful if the NSEC is from the parent */
1942 r
= dnssec_nsec_from_parent_zone(rr
, name
);
1948 /* Check if this NSEC RR proves the absence of an explicit RR under this name */
1949 r
= dnssec_nsec_covers(rr
, name
);
1952 if (r
> 0 && (!covering_rr
|| !covering_rr_authenticated
)) {
1954 covering_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1957 /* Check if this NSEC RR proves the absence of a wildcard RR under this name */
1958 r
= dnssec_nsec_covers_wildcard(rr
, name
);
1961 if (r
> 0 && (!wildcard_rr
|| !wildcard_rr_authenticated
)) {
1963 wildcard_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1967 if (covering_rr
&& wildcard_rr
) {
1968 /* If we could prove that neither the name itself, nor the wildcard at the closest encloser exists, we
1969 * proved the NXDOMAIN case. */
1970 *result
= DNSSEC_NSEC_NXDOMAIN
;
1973 *authenticated
= covering_rr_authenticated
&& wildcard_rr_authenticated
;
1975 *ttl
= MIN(covering_rr
->ttl
, wildcard_rr
->ttl
);
1980 /* OK, this was not sufficient. Let's see if NSEC3 can help. */
1982 return dnssec_test_nsec3(answer
, key
, result
, authenticated
, ttl
);
1984 /* No approproate NSEC RR found, report this. */
1985 *result
= DNSSEC_NSEC_NO_RR
;
1989 static int dnssec_nsec_test_enclosed(DnsAnswer
*answer
, uint16_t type
, const char *name
, const char *zone
, bool *authenticated
) {
1990 DnsResourceRecord
*rr
;
1991 DnsAnswerFlags flags
;
1997 /* Checks whether there's an NSEC/NSEC3 that proves that the specified 'name' is non-existing in the specified
1998 * 'zone'. The 'zone' must be a suffix of the 'name'. */
2000 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
2003 if (rr
->key
->type
!= type
&& type
!= DNS_TYPE_ANY
)
2006 switch (rr
->key
->type
) {
2010 /* We only care for NSEC RRs from the indicated zone */
2011 r
= dns_resource_record_is_signer(rr
, zone
);
2017 r
= dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
2024 case DNS_TYPE_NSEC3
: {
2025 _cleanup_free_
char *hashed_domain
= NULL
, *next_hashed_domain
= NULL
;
2027 /* We only care for NSEC3 RRs from the indicated zone */
2028 r
= dns_resource_record_is_signer(rr
, zone
);
2034 r
= nsec3_is_good(rr
, NULL
);
2040 /* Format the domain we are testing with the NSEC3 RR's hash function */
2041 r
= nsec3_hashed_domain_make(
2048 if ((size_t) r
!= rr
->nsec3
.next_hashed_name_size
)
2051 /* Format the NSEC3's next hashed name as proper domain name */
2052 r
= nsec3_hashed_domain_format(
2053 rr
->nsec3
.next_hashed_name
,
2054 rr
->nsec3
.next_hashed_name_size
,
2056 &next_hashed_domain
);
2060 r
= dns_name_between(dns_resource_key_name(rr
->key
), hashed_domain
, next_hashed_domain
);
2074 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2082 static int dnssec_test_positive_wildcard_nsec3(
2087 bool *authenticated
) {
2089 const char *next_closer
= NULL
;
2092 /* Run a positive NSEC3 wildcard proof. Specifically:
2094 * A proof that the "next closer" of the generating wildcard does not exist.
2096 * Note a key difference between the NSEC3 and NSEC versions of the proof. NSEC RRs don't have to exist for
2097 * empty non-transients. NSEC3 RRs however have to. This means it's sufficient to check if the next closer name
2098 * exists for the NSEC3 RR and we are done.
2100 * 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
2101 * c.d.e.f does not exist. */
2105 r
= dns_name_parent(&name
);
2111 r
= dns_name_equal(name
, source
);
2118 return dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC3
, next_closer
, zone
, authenticated
);
2121 static int dnssec_test_positive_wildcard_nsec(
2126 bool *_authenticated
) {
2128 bool authenticated
= true;
2131 /* Run a positive NSEC wildcard proof. Specifically:
2133 * A proof that there's neither a wildcard name nor a non-wildcard name that is a suffix of the name "name" and
2134 * a prefix of the synthesizing source "source" in the zone "zone".
2136 * See RFC 5155, Section 8.8 and RFC 4035, Section 5.3.4
2138 * 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
2139 * have to prove that none of the following exist:
2149 _cleanup_free_
char *wc
= NULL
;
2152 /* Check if there's an NSEC or NSEC3 RR that proves that the mame we determined is really non-existing,
2153 * i.e between the owner name and the next name of an NSEC RR. */
2154 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, name
, zone
, &a
);
2158 authenticated
= authenticated
&& a
;
2160 /* Strip one label off */
2161 r
= dns_name_parent(&name
);
2165 /* Did we reach the source of synthesis? */
2166 r
= dns_name_equal(name
, source
);
2170 /* Successful exit */
2171 *_authenticated
= authenticated
;
2175 /* Safety check, that the source of synthesis is still our suffix */
2176 r
= dns_name_endswith(name
, source
);
2182 /* Replace the label we stripped off with an asterisk */
2183 wc
= strappend("*.", name
);
2187 /* And check if the proof holds for the asterisk name, too */
2188 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, wc
, zone
, &a
);
2192 authenticated
= authenticated
&& a
;
2193 /* In the next iteration we'll check the non-asterisk-prefixed version */
2197 int dnssec_test_positive_wildcard(
2202 bool *authenticated
) {
2209 assert(authenticated
);
2211 r
= dns_answer_contains_zone_nsec3(answer
, zone
);
2215 return dnssec_test_positive_wildcard_nsec3(answer
, name
, source
, zone
, authenticated
);
2217 return dnssec_test_positive_wildcard_nsec(answer
, name
, source
, zone
, authenticated
);
2222 int dnssec_verify_rrset(
2224 const DnsResourceKey
*key
,
2225 DnsResourceRecord
*rrsig
,
2226 DnsResourceRecord
*dnskey
,
2228 DnssecResult
*result
) {
2233 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
2238 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
2243 int dnssec_verify_rrset_search(
2245 const DnsResourceKey
*key
,
2246 DnsAnswer
*validated_dnskeys
,
2248 DnssecResult
*result
,
2249 DnsResourceRecord
**ret_rrsig
) {
2254 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
2259 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
2264 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
2269 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
2274 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
2279 int dnssec_test_positive_wildcard(
2284 bool *authenticated
) {
2291 static const char* const dnssec_result_table
[_DNSSEC_RESULT_MAX
] = {
2292 [DNSSEC_VALIDATED
] = "validated",
2293 [DNSSEC_VALIDATED_WILDCARD
] = "validated-wildcard",
2294 [DNSSEC_INVALID
] = "invalid",
2295 [DNSSEC_SIGNATURE_EXPIRED
] = "signature-expired",
2296 [DNSSEC_UNSUPPORTED_ALGORITHM
] = "unsupported-algorithm",
2297 [DNSSEC_NO_SIGNATURE
] = "no-signature",
2298 [DNSSEC_MISSING_KEY
] = "missing-key",
2299 [DNSSEC_UNSIGNED
] = "unsigned",
2300 [DNSSEC_FAILED_AUXILIARY
] = "failed-auxiliary",
2301 [DNSSEC_NSEC_MISMATCH
] = "nsec-mismatch",
2302 [DNSSEC_INCOMPATIBLE_SERVER
] = "incompatible-server",
2304 DEFINE_STRING_TABLE_LOOKUP(dnssec_result
, DnssecResult
);
2306 static const char* const dnssec_verdict_table
[_DNSSEC_VERDICT_MAX
] = {
2307 [DNSSEC_SECURE
] = "secure",
2308 [DNSSEC_INSECURE
] = "insecure",
2309 [DNSSEC_BOGUS
] = "bogus",
2310 [DNSSEC_INDETERMINATE
] = "indeterminate",
2312 DEFINE_STRING_TABLE_LOOKUP(dnssec_verdict
, DnssecVerdict
);