1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
4 #include "dns-domain.h"
7 #include "gcrypt-util.h"
9 #include "memory-util.h"
10 #include "memstream-util.h"
11 #include "openssl-util.h"
12 #include "resolved-dns-dnssec.h"
13 #include "resolved-dns-packet.h"
14 #include "sort-util.h"
15 #include "string-table.h"
17 #if PREFER_OPENSSL && OPENSSL_VERSION_MAJOR >= 3
18 # pragma GCC diagnostic push
19 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
20 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(RSA
*, RSA_free
, NULL
);
21 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(EC_KEY
*, EC_KEY_free
, NULL
);
22 # pragma GCC diagnostic pop
25 #define VERIFY_RRS_MAX 256
26 #define MAX_KEY_SIZE (32*1024)
28 /* Permit a maximum clock skew of 1h 10min. This should be enough to deal with DST confusion */
29 #define SKEW_MAX (1*USEC_PER_HOUR + 10*USEC_PER_MINUTE)
31 /* Maximum number of NSEC3 iterations we'll do. RFC5155 says 2500 shall be the maximum useful value, but
32 * RFC9276 § 3.2 says that we should reduce the acceptable iteration count */
33 #define NSEC3_ITERATIONS_MAX 100
36 * The DNSSEC Chain of trust:
38 * Normal RRs are protected via RRSIG RRs in combination with DNSKEY RRs, all in the same zone
39 * DNSKEY RRs are either protected like normal RRs, or via a DS from a zone "higher" up the tree
40 * DS RRs are protected like normal RRs
43 * Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS
46 uint16_t dnssec_keytag(DnsResourceRecord
*dnskey
, bool mask_revoke
) {
50 /* The algorithm from RFC 4034, Appendix B. */
53 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
55 f
= (uint32_t) dnskey
->dnskey
.flags
;
58 f
&= ~DNSKEY_FLAG_REVOKE
;
60 sum
= f
+ ((((uint32_t) dnskey
->dnskey
.protocol
) << 8) + (uint32_t) dnskey
->dnskey
.algorithm
);
62 p
= dnskey
->dnskey
.key
;
64 for (size_t i
= 0; i
< dnskey
->dnskey
.key_size
; i
++)
65 sum
+= (i
& 1) == 0 ? (uint32_t) p
[i
] << 8 : (uint32_t) p
[i
];
67 sum
+= (sum
>> 16) & UINT32_C(0xFFFF);
69 return sum
& UINT32_C(0xFFFF);
72 #if HAVE_OPENSSL_OR_GCRYPT
74 static int rr_compare(DnsResourceRecord
* const *a
, DnsResourceRecord
* const *b
) {
75 const DnsResourceRecord
*x
= *a
, *y
= *b
;
79 /* Let's order the RRs according to RFC 4034, Section 6.3 */
82 assert(x
->wire_format
);
84 assert(y
->wire_format
);
86 m
= MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(x
), DNS_RESOURCE_RECORD_RDATA_SIZE(y
));
88 r
= memcmp(DNS_RESOURCE_RECORD_RDATA(x
), DNS_RESOURCE_RECORD_RDATA(y
), m
);
92 return CMP(DNS_RESOURCE_RECORD_RDATA_SIZE(x
), DNS_RESOURCE_RECORD_RDATA_SIZE(y
));
95 static int dnssec_rsa_verify_raw(
96 hash_algorithm_t hash_algorithm
,
97 const void *signature
, size_t signature_size
,
98 const void *data
, size_t data_size
,
99 const void *exponent
, size_t exponent_size
,
100 const void *modulus
, size_t modulus_size
) {
104 # pragma GCC diagnostic push
105 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
106 _cleanup_(RSA_freep
) RSA
*rpubkey
= NULL
;
107 _cleanup_(EVP_PKEY_freep
) EVP_PKEY
*epubkey
= NULL
;
108 _cleanup_(EVP_PKEY_CTX_freep
) EVP_PKEY_CTX
*ctx
= NULL
;
109 _cleanup_(BN_freep
) BIGNUM
*e
= NULL
, *m
= NULL
;
111 assert(hash_algorithm
);
113 e
= BN_bin2bn(exponent
, exponent_size
, NULL
);
117 m
= BN_bin2bn(modulus
, modulus_size
, NULL
);
125 if (RSA_set0_key(rpubkey
, m
, e
, NULL
) <= 0)
129 assert((size_t) RSA_size(rpubkey
) == signature_size
);
131 epubkey
= EVP_PKEY_new();
135 if (EVP_PKEY_assign_RSA(epubkey
, RSAPublicKey_dup(rpubkey
)) <= 0)
138 ctx
= EVP_PKEY_CTX_new(epubkey
, NULL
);
142 if (EVP_PKEY_verify_init(ctx
) <= 0)
145 if (EVP_PKEY_CTX_set_rsa_padding(ctx
, RSA_PKCS1_PADDING
) <= 0)
148 if (EVP_PKEY_CTX_set_signature_md(ctx
, hash_algorithm
) <= 0)
151 r
= EVP_PKEY_verify(ctx
, signature
, signature_size
, data
, data_size
);
153 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
154 "Signature verification failed: 0x%lx", ERR_get_error());
156 # pragma GCC diagnostic pop
158 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
159 gcry_mpi_t n
= NULL
, e
= NULL
, s
= NULL
;
162 assert(hash_algorithm
);
164 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature
, signature_size
, NULL
);
170 ge
= gcry_mpi_scan(&e
, GCRYMPI_FMT_USG
, exponent
, exponent_size
, NULL
);
176 ge
= gcry_mpi_scan(&n
, GCRYMPI_FMT_USG
, modulus
, modulus_size
, NULL
);
182 ge
= gcry_sexp_build(&signature_sexp
,
184 "(sig-val (rsa (s %m)))",
192 ge
= gcry_sexp_build(&data_sexp
,
194 "(data (flags pkcs1) (hash %s %b))",
203 ge
= gcry_sexp_build(&public_key_sexp
,
205 "(public-key (rsa (n %m) (e %m)))",
213 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
214 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
217 r
= log_debug_errno(SYNTHETIC_ERRNO(EIO
),
218 "RSA signature check failed: %s", gpg_strerror(ge
));
231 gcry_sexp_release(public_key_sexp
);
233 gcry_sexp_release(signature_sexp
);
235 gcry_sexp_release(data_sexp
);
240 static int dnssec_rsa_verify(
241 hash_algorithm_t hash_algorithm
,
242 const void *hash
, size_t hash_size
,
243 DnsResourceRecord
*rrsig
,
244 DnsResourceRecord
*dnskey
) {
246 size_t exponent_size
, modulus_size
;
247 void *exponent
, *modulus
;
249 assert(hash_algorithm
);
251 assert(hash_size
> 0);
255 if (*(uint8_t*) dnskey
->dnskey
.key
== 0) {
256 /* exponent is > 255 bytes long */
258 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 3;
260 ((size_t) (((uint8_t*) dnskey
->dnskey
.key
)[1]) << 8) |
261 ((size_t) ((uint8_t*) dnskey
->dnskey
.key
)[2]);
263 if (exponent_size
< 256)
266 if (3 + exponent_size
>= dnskey
->dnskey
.key_size
)
269 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 3 + exponent_size
;
270 modulus_size
= dnskey
->dnskey
.key_size
- 3 - exponent_size
;
273 /* exponent is <= 255 bytes long */
275 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 1;
276 exponent_size
= (size_t) ((uint8_t*) dnskey
->dnskey
.key
)[0];
278 if (exponent_size
<= 0)
281 if (1 + exponent_size
>= dnskey
->dnskey
.key_size
)
284 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 1 + exponent_size
;
285 modulus_size
= dnskey
->dnskey
.key_size
- 1 - exponent_size
;
288 return dnssec_rsa_verify_raw(
290 rrsig
->rrsig
.signature
, rrsig
->rrsig
.signature_size
,
292 exponent
, exponent_size
,
293 modulus
, modulus_size
);
296 static int dnssec_ecdsa_verify_raw(
297 hash_algorithm_t hash_algorithm
,
298 elliptic_curve_t curve
,
299 const void *signature_r
, size_t signature_r_size
,
300 const void *signature_s
, size_t signature_s_size
,
301 const void *data
, size_t data_size
,
302 const void *key
, size_t key_size
) {
306 # pragma GCC diagnostic push
307 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
308 _cleanup_(EC_GROUP_freep
) EC_GROUP
*ec_group
= NULL
;
309 _cleanup_(EC_POINT_freep
) EC_POINT
*p
= NULL
;
310 _cleanup_(EC_KEY_freep
) EC_KEY
*eckey
= NULL
;
311 _cleanup_(BN_CTX_freep
) BN_CTX
*bctx
= NULL
;
312 _cleanup_(BN_freep
) BIGNUM
*r
= NULL
, *s
= NULL
;
313 _cleanup_(ECDSA_SIG_freep
) ECDSA_SIG
*sig
= NULL
;
315 assert(hash_algorithm
);
317 ec_group
= EC_GROUP_new_by_curve_name(curve
);
321 p
= EC_POINT_new(ec_group
);
329 if (EC_POINT_oct2point(ec_group
, p
, key
, key_size
, bctx
) <= 0)
332 eckey
= EC_KEY_new();
336 if (EC_KEY_set_group(eckey
, ec_group
) <= 0)
339 if (EC_KEY_set_public_key(eckey
, p
) <= 0)
340 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
341 "EC_POINT_bn2point failed: 0x%lx", ERR_get_error());
343 assert(EC_KEY_check_key(eckey
) == 1);
345 r
= BN_bin2bn(signature_r
, signature_r_size
, NULL
);
349 s
= BN_bin2bn(signature_s
, signature_s_size
, NULL
);
353 /* TODO: We should eventually use the EVP API once it supports ECDSA signature verification */
355 sig
= ECDSA_SIG_new();
359 if (ECDSA_SIG_set0(sig
, r
, s
) <= 0)
363 k
= ECDSA_do_verify(data
, data_size
, sig
, eckey
);
365 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
366 "Signature verification failed: 0x%lx", ERR_get_error());
368 # pragma GCC diagnostic pop
370 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
371 gcry_mpi_t q
= NULL
, r
= NULL
, s
= NULL
;
374 assert(hash_algorithm
);
376 ge
= gcry_mpi_scan(&r
, GCRYMPI_FMT_USG
, signature_r
, signature_r_size
, NULL
);
382 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature_s
, signature_s_size
, NULL
);
388 ge
= gcry_mpi_scan(&q
, GCRYMPI_FMT_USG
, key
, key_size
, NULL
);
394 ge
= gcry_sexp_build(&signature_sexp
,
396 "(sig-val (ecdsa (r %m) (s %m)))",
404 ge
= gcry_sexp_build(&data_sexp
,
406 "(data (flags rfc6979) (hash %s %b))",
415 ge
= gcry_sexp_build(&public_key_sexp
,
417 "(public-key (ecc (curve %s) (q %m)))",
425 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
426 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
429 log_debug("ECDSA signature check failed: %s", gpg_strerror(ge
));
442 gcry_sexp_release(public_key_sexp
);
444 gcry_sexp_release(signature_sexp
);
446 gcry_sexp_release(data_sexp
);
451 static int dnssec_ecdsa_verify(
452 hash_algorithm_t hash_algorithm
,
454 const void *hash
, size_t hash_size
,
455 DnsResourceRecord
*rrsig
,
456 DnsResourceRecord
*dnskey
) {
458 elliptic_curve_t curve
;
467 if (algorithm
== DNSSEC_ALGORITHM_ECDSAP256SHA256
) {
468 curve
= OPENSSL_OR_GCRYPT(NID_X9_62_prime256v1
, "NIST P-256"); /* NIST P-256 */
470 } else if (algorithm
== DNSSEC_ALGORITHM_ECDSAP384SHA384
) {
471 curve
= OPENSSL_OR_GCRYPT(NID_secp384r1
, "NIST P-384"); /* NIST P-384 */
476 if (dnskey
->dnskey
.key_size
!= key_size
* 2)
479 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
482 q
= newa(uint8_t, key_size
*2 + 1);
483 q
[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
484 memcpy(q
+1, dnskey
->dnskey
.key
, key_size
*2);
486 return dnssec_ecdsa_verify_raw(
489 rrsig
->rrsig
.signature
, key_size
,
490 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
495 static int dnssec_eddsa_verify_raw(
496 elliptic_curve_t curve
,
497 const uint8_t *signature
, size_t signature_size
,
498 const uint8_t *data
, size_t data_size
,
499 const uint8_t *key
, size_t key_size
) {
502 _cleanup_(EVP_PKEY_freep
) EVP_PKEY
*evkey
= NULL
;
503 _cleanup_(EVP_PKEY_CTX_freep
) EVP_PKEY_CTX
*pctx
= NULL
;
504 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= NULL
;
507 assert(curve
== NID_ED25519
);
508 assert(signature_size
== key_size
* 2);
510 uint8_t *q
= newa(uint8_t, signature_size
+ 1);
511 q
[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
512 memcpy(q
+1, signature
, signature_size
);
514 evkey
= EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519
, NULL
, key
, key_size
);
516 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
517 "EVP_PKEY_new_raw_public_key failed: 0x%lx", ERR_get_error());
519 pctx
= EVP_PKEY_CTX_new(evkey
, NULL
);
523 ctx
= EVP_MD_CTX_new();
527 /* This prevents EVP_DigestVerifyInit from managing pctx and complicating our free logic. */
528 EVP_MD_CTX_set_pkey_ctx(ctx
, pctx
);
530 /* One might be tempted to use EVP_PKEY_verify_init, but see Ed25519(7ssl). */
531 if (EVP_DigestVerifyInit(ctx
, &pctx
, NULL
, NULL
, evkey
) <= 0)
534 r
= EVP_DigestVerify(ctx
, signature
, signature_size
, data
, data_size
);
536 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
537 "Signature verification failed: 0x%lx", ERR_get_error());
541 #elif GCRYPT_VERSION_NUMBER >= 0x010600
542 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
546 assert(signature_size
== key_size
* 2);
548 ge
= gcry_sexp_build(&signature_sexp
,
550 "(sig-val (eddsa (r %b) (s %b)))",
554 signature
+ key_size
);
560 ge
= gcry_sexp_build(&data_sexp
,
562 "(data (flags eddsa) (hash-algo sha512) (value %b))",
570 ge
= gcry_sexp_build(&public_key_sexp
,
572 "(public-key (ecc (curve %s) (flags eddsa) (q %b)))",
581 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
582 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
585 k
= log_debug_errno(SYNTHETIC_ERRNO(EIO
),
586 "EdDSA signature check failed: %s", gpg_strerror(ge
));
591 gcry_sexp_release(public_key_sexp
);
593 gcry_sexp_release(signature_sexp
);
595 gcry_sexp_release(data_sexp
);
603 static int dnssec_eddsa_verify(
605 const void *data
, size_t data_size
,
606 DnsResourceRecord
*rrsig
,
607 DnsResourceRecord
*dnskey
) {
608 elliptic_curve_t curve
;
611 if (algorithm
== DNSSEC_ALGORITHM_ED25519
) {
612 curve
= OPENSSL_OR_GCRYPT(NID_ED25519
, "Ed25519");
617 if (dnskey
->dnskey
.key_size
!= key_size
)
620 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
623 return dnssec_eddsa_verify_raw(
625 rrsig
->rrsig
.signature
, rrsig
->rrsig
.signature_size
,
627 dnskey
->dnskey
.key
, key_size
);
630 static int md_add_uint8(hash_context_t ctx
, uint8_t v
) {
632 return EVP_DigestUpdate(ctx
, &v
, sizeof(v
));
634 gcry_md_write(ctx
, &v
, sizeof(v
));
639 static int md_add_uint16(hash_context_t ctx
, uint16_t v
) {
642 return EVP_DigestUpdate(ctx
, &v
, sizeof(v
));
644 gcry_md_write(ctx
, &v
, sizeof(v
));
649 static void fwrite_uint8(FILE *fp
, uint8_t v
) {
650 fwrite(&v
, sizeof(v
), 1, fp
);
653 static void fwrite_uint16(FILE *fp
, uint16_t v
) {
655 fwrite(&v
, sizeof(v
), 1, fp
);
658 static void fwrite_uint32(FILE *fp
, uint32_t v
) {
660 fwrite(&v
, sizeof(v
), 1, fp
);
663 static int dnssec_rrsig_prepare(DnsResourceRecord
*rrsig
) {
664 int n_key_labels
, n_signer_labels
;
668 /* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source
669 * and .n_skip_labels_signer fields so that we can use them later on. */
672 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
674 /* Check if this RRSIG RR is already prepared */
675 if (rrsig
->n_skip_labels_source
!= UINT8_MAX
)
678 if (rrsig
->rrsig
.inception
> rrsig
->rrsig
.expiration
)
681 name
= dns_resource_key_name(rrsig
->key
);
683 n_key_labels
= dns_name_count_labels(name
);
684 if (n_key_labels
< 0)
686 if (rrsig
->rrsig
.labels
> n_key_labels
)
689 n_signer_labels
= dns_name_count_labels(rrsig
->rrsig
.signer
);
690 if (n_signer_labels
< 0)
691 return n_signer_labels
;
692 if (n_signer_labels
> rrsig
->rrsig
.labels
)
695 r
= dns_name_skip(name
, n_key_labels
- n_signer_labels
, &name
);
701 /* Check if the signer is really a suffix of us */
702 r
= dns_name_equal(name
, rrsig
->rrsig
.signer
);
708 assert(n_key_labels
< UINT8_MAX
); /* UINT8_MAX/-1 means unsigned. */
709 rrsig
->n_skip_labels_source
= n_key_labels
- rrsig
->rrsig
.labels
;
710 rrsig
->n_skip_labels_signer
= n_key_labels
- n_signer_labels
;
715 static int dnssec_rrsig_expired(DnsResourceRecord
*rrsig
, usec_t realtime
) {
716 usec_t expiration
, inception
, skew
;
719 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
721 if (realtime
== USEC_INFINITY
)
722 realtime
= now(CLOCK_REALTIME
);
724 expiration
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
725 inception
= rrsig
->rrsig
.inception
* USEC_PER_SEC
;
727 /* Consider inverted validity intervals as expired */
728 if (inception
> expiration
)
731 /* Permit a certain amount of clock skew of 10% of the valid
732 * time range. This takes inspiration from unbound's
734 skew
= (expiration
- inception
) / 10;
738 if (inception
< skew
)
743 if (expiration
+ skew
< expiration
)
744 expiration
= USEC_INFINITY
;
748 return realtime
< inception
|| realtime
> expiration
;
751 static hash_md_t
algorithm_to_implementation_id(uint8_t algorithm
) {
753 /* Translates a DNSSEC signature algorithm into an openssl/gcrypt digest identifier.
755 * Note that we implement all algorithms listed as "Must implement" and "Recommended to Implement" in
756 * RFC6944. We don't implement any algorithms that are listed as "Optional" or "Must Not Implement".
757 * Specifically, we do not implement RSAMD5, DSASHA1, DH, DSA-NSEC3-SHA1, and GOST-ECC. */
761 case DNSSEC_ALGORITHM_RSASHA1
:
762 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
763 return OPENSSL_OR_GCRYPT(EVP_sha1(), GCRY_MD_SHA1
);
765 case DNSSEC_ALGORITHM_RSASHA256
:
766 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
767 return OPENSSL_OR_GCRYPT(EVP_sha256(), GCRY_MD_SHA256
);
769 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
770 return OPENSSL_OR_GCRYPT(EVP_sha384(), GCRY_MD_SHA384
);
772 case DNSSEC_ALGORITHM_RSASHA512
:
773 return OPENSSL_OR_GCRYPT(EVP_sha512(), GCRY_MD_SHA512
);
776 return OPENSSL_OR_GCRYPT(NULL
, -EOPNOTSUPP
);
780 static void dnssec_fix_rrset_ttl(
781 DnsResourceRecord
*list
[],
783 DnsResourceRecord
*rrsig
) {
789 for (unsigned k
= 0; k
< n
; k
++) {
790 DnsResourceRecord
*rr
= list
[k
];
792 /* Pick the TTL as the minimum of the RR's TTL, the
793 * RR's original TTL according to the RRSIG and the
794 * RRSIG's own TTL, see RFC 4035, Section 5.3.3 */
795 rr
->ttl
= MIN3(rr
->ttl
, rrsig
->rrsig
.original_ttl
, rrsig
->ttl
);
796 rr
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
798 /* Copy over information about the signer and wildcard source of synthesis */
799 rr
->n_skip_labels_source
= rrsig
->n_skip_labels_source
;
800 rr
->n_skip_labels_signer
= rrsig
->n_skip_labels_signer
;
803 rrsig
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
806 static int dnssec_rrset_serialize_sig(
807 DnsResourceRecord
*rrsig
,
809 DnsResourceRecord
**list
,
813 size_t *ret_sig_size
) {
815 _cleanup_(memstream_done
) MemStream m
= {};
816 uint8_t wire_format_name
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
817 DnsResourceRecord
*rr
;
823 assert(list
|| list_len
== 0);
824 assert(ret_sig_data
);
825 assert(ret_sig_size
);
827 f
= memstream_init(&m
);
831 fwrite_uint16(f
, rrsig
->rrsig
.type_covered
);
832 fwrite_uint8(f
, rrsig
->rrsig
.algorithm
);
833 fwrite_uint8(f
, rrsig
->rrsig
.labels
);
834 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
835 fwrite_uint32(f
, rrsig
->rrsig
.expiration
);
836 fwrite_uint32(f
, rrsig
->rrsig
.inception
);
837 fwrite_uint16(f
, rrsig
->rrsig
.key_tag
);
839 r
= dns_name_to_wire_format(rrsig
->rrsig
.signer
, wire_format_name
, sizeof(wire_format_name
), true);
842 fwrite(wire_format_name
, 1, r
, f
);
844 /* Convert the source of synthesis into wire format */
845 r
= dns_name_to_wire_format(source
, wire_format_name
, sizeof(wire_format_name
), true);
849 for (size_t k
= 0; k
< list_len
; k
++) {
854 /* Hash the source of synthesis. If this is a wildcard, then prefix it with the *. label */
856 fwrite((uint8_t[]) { 1, '*'}, sizeof(uint8_t), 2, f
);
857 fwrite(wire_format_name
, 1, r
, f
);
859 fwrite_uint16(f
, rr
->key
->type
);
860 fwrite_uint16(f
, rr
->key
->class);
861 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
863 l
= DNS_RESOURCE_RECORD_RDATA_SIZE(rr
);
866 fwrite_uint16(f
, (uint16_t) l
);
867 fwrite(DNS_RESOURCE_RECORD_RDATA(rr
), 1, l
, f
);
870 return memstream_finalize(&m
, ret_sig_data
, ret_sig_size
);
873 static int dnssec_rrset_verify_sig(
874 DnsResourceRecord
*rrsig
,
875 DnsResourceRecord
*dnskey
,
876 const char *sig_data
,
882 assert(sig_size
> 0);
884 hash_md_t md_algorithm
;
887 uint8_t hash
[EVP_MAX_MD_SIZE
];
890 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
894 initialize_libgcrypt(false);
897 switch (rrsig
->rrsig
.algorithm
) {
898 case DNSSEC_ALGORITHM_ED25519
:
899 #if PREFER_OPENSSL || GCRYPT_VERSION_NUMBER >= 0x010600
900 return dnssec_eddsa_verify(
901 rrsig
->rrsig
.algorithm
,
906 case DNSSEC_ALGORITHM_ED448
:
909 /* OK, the RRs are now in canonical order. Let's calculate the digest */
910 md_algorithm
= algorithm_to_implementation_id(rrsig
->rrsig
.algorithm
);
915 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
919 if (EVP_DigestInit_ex(ctx
, md_algorithm
, NULL
) <= 0)
922 if (EVP_DigestUpdate(ctx
, sig_data
, sig_size
) <= 0)
925 if (EVP_DigestFinal_ex(ctx
, hash
, &hash_size
) <= 0)
928 assert(hash_size
> 0);
931 if (md_algorithm
< 0)
934 gcry_error_t err
= gcry_md_open(&md
, md_algorithm
, 0);
935 if (gcry_err_code(err
) != GPG_ERR_NO_ERROR
|| !md
)
938 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
939 assert(hash_size
> 0);
941 gcry_md_write(md
, sig_data
, sig_size
);
943 hash
= gcry_md_read(md
, 0);
949 switch (rrsig
->rrsig
.algorithm
) {
951 case DNSSEC_ALGORITHM_RSASHA1
:
952 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
953 case DNSSEC_ALGORITHM_RSASHA256
:
954 case DNSSEC_ALGORITHM_RSASHA512
:
955 return dnssec_rsa_verify(
956 OPENSSL_OR_GCRYPT(md_algorithm
, gcry_md_algo_name(md_algorithm
)),
961 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
962 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
963 return dnssec_ecdsa_verify(
964 OPENSSL_OR_GCRYPT(md_algorithm
, gcry_md_algo_name(md_algorithm
)),
965 rrsig
->rrsig
.algorithm
,
971 assert_not_reached();
975 int dnssec_verify_rrset(
977 const DnsResourceKey
*key
,
978 DnsResourceRecord
*rrsig
,
979 DnsResourceRecord
*dnskey
,
981 DnssecResult
*result
) {
983 DnsResourceRecord
**list
, *rr
;
984 const char *source
, *name
;
985 _cleanup_free_
char *sig_data
= NULL
;
986 size_t sig_size
= 0; /* avoid false maybe-uninitialized warning */
995 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
996 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
998 /* Verifies that the RRSet matches the specified "key" in "a",
999 * using the signature "rrsig" and the key "dnskey". It's
1000 * assumed that RRSIG and DNSKEY match. */
1002 r
= dnssec_rrsig_prepare(rrsig
);
1004 *result
= DNSSEC_INVALID
;
1010 r
= dnssec_rrsig_expired(rrsig
, realtime
);
1014 *result
= DNSSEC_SIGNATURE_EXPIRED
;
1018 name
= dns_resource_key_name(key
);
1020 /* Some keys may only appear signed in the zone apex, and are invalid anywhere else. (SOA, NS...) */
1021 if (dns_type_apex_only(rrsig
->rrsig
.type_covered
)) {
1022 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
1026 *result
= DNSSEC_INVALID
;
1031 /* OTOH DS RRs may not appear in the zone apex, but are valid everywhere else. */
1032 if (rrsig
->rrsig
.type_covered
== DNS_TYPE_DS
) {
1033 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
1037 *result
= DNSSEC_INVALID
;
1042 /* Determine the "Source of Synthesis" and whether this is a wildcard RRSIG */
1043 r
= dns_name_suffix(name
, rrsig
->rrsig
.labels
, &source
);
1046 if (r
> 0 && !dns_type_may_wildcard(rrsig
->rrsig
.type_covered
)) {
1047 /* We refuse to validate NSEC3 or SOA RRs that are synthesized from wildcards */
1048 *result
= DNSSEC_INVALID
;
1052 /* If we stripped a single label, then let's see if that maybe was "*". If so, we are not really
1053 * synthesized from a wildcard, we are the wildcard itself. Treat that like a normal name. */
1054 r
= dns_name_startswith(name
, "*");
1064 /* Collect all relevant RRs in a single array, so that we can look at the RRset */
1065 list
= newa(DnsResourceRecord
*, dns_answer_size(a
));
1067 DNS_ANSWER_FOREACH(rr
, a
) {
1068 r
= dns_resource_key_equal(key
, rr
->key
);
1074 /* We need the wire format for ordering, and digest calculation */
1075 r
= dns_resource_record_to_wire_format(rr
, true);
1081 if (n
> VERIFY_RRS_MAX
)
1088 /* Bring the RRs into canonical order */
1089 typesafe_qsort(list
, n
, rr_compare
);
1091 r
= dnssec_rrset_serialize_sig(rrsig
, source
, list
, n
, wildcard
,
1092 &sig_data
, &sig_size
);
1096 r
= dnssec_rrset_verify_sig(rrsig
, dnskey
, sig_data
, sig_size
);
1097 if (r
== -EOPNOTSUPP
) {
1098 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
1104 /* Now, fix the ttl, expiry, and remember the synthesizing source and the signer */
1106 dnssec_fix_rrset_ttl(list
, n
, rrsig
);
1109 *result
= DNSSEC_INVALID
;
1111 *result
= DNSSEC_VALIDATED_WILDCARD
;
1113 *result
= DNSSEC_VALIDATED
;
1118 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
1123 /* Checks if the specified DNSKEY RR matches the key used for
1124 * the signature in the specified RRSIG RR */
1126 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
1129 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1131 if (dnskey
->key
->class != rrsig
->key
->class)
1133 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
1135 if (!revoked_ok
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
1137 if (dnskey
->dnskey
.protocol
!= 3)
1139 if (dnskey
->dnskey
.algorithm
!= rrsig
->rrsig
.algorithm
)
1142 if (dnssec_keytag(dnskey
, false) != rrsig
->rrsig
.key_tag
)
1145 return dns_name_equal(dns_resource_key_name(dnskey
->key
), rrsig
->rrsig
.signer
);
1148 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
1152 /* Checks if the specified RRSIG RR protects the RRSet of the specified RR key. */
1154 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
1156 if (rrsig
->key
->class != key
->class)
1158 if (rrsig
->rrsig
.type_covered
!= key
->type
)
1161 return dns_name_equal(dns_resource_key_name(rrsig
->key
), dns_resource_key_name(key
));
1164 int dnssec_verify_rrset_search(
1166 const DnsResourceKey
*key
,
1167 DnsAnswer
*validated_dnskeys
,
1169 DnssecResult
*result
,
1170 DnsResourceRecord
**ret_rrsig
) {
1172 bool found_rrsig
= false, found_invalid
= false, found_expired_rrsig
= false, found_unsupported_algorithm
= false;
1173 unsigned nvalidations
= 0;
1174 DnsResourceRecord
*rrsig
;
1180 /* Verifies all RRs from "a" that match the key "key" against DNSKEYs in "validated_dnskeys" */
1182 if (dns_answer_isempty(a
))
1185 /* Iterate through each RRSIG RR. */
1186 DNS_ANSWER_FOREACH(rrsig
, a
) {
1187 DnsResourceRecord
*dnskey
;
1188 DnsAnswerFlags flags
;
1190 /* Is this an RRSIG RR that applies to RRs matching our key? */
1191 r
= dnssec_key_match_rrsig(key
, rrsig
);
1199 /* Look for a matching key */
1200 DNS_ANSWER_FOREACH_FLAGS(dnskey
, flags
, validated_dnskeys
) {
1201 DnssecResult one_result
;
1203 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1206 /* Is this a DNSKEY RR that matches they key of our RRSIG? */
1207 r
= dnssec_rrsig_match_dnskey(rrsig
, dnskey
, false);
1213 /* Take the time here, if it isn't set yet, so
1214 * that we do all validations with the same
1216 if (realtime
== USEC_INFINITY
)
1217 realtime
= now(CLOCK_REALTIME
);
1219 /* Have we seen an unreasonable number of invalid signaures? */
1220 if (nvalidations
> DNSSEC_INVALID_MAX
) {
1223 *result
= DNSSEC_TOO_MANY_VALIDATIONS
;
1224 return (int) nvalidations
;
1227 /* Yay, we found a matching RRSIG with a matching
1228 * DNSKEY, awesome. Now let's verify all entries of
1229 * the RRSet against the RRSIG and DNSKEY
1232 r
= dnssec_verify_rrset(a
, key
, rrsig
, dnskey
, realtime
, &one_result
);
1238 switch (one_result
) {
1240 case DNSSEC_VALIDATED
:
1241 case DNSSEC_VALIDATED_WILDCARD
:
1242 /* Yay, the RR has been validated,
1243 * return immediately, but fix up the expiry */
1247 *result
= one_result
;
1248 return (int) nvalidations
;
1250 case DNSSEC_INVALID
:
1251 /* If the signature is invalid, let's try another
1252 key and/or signature. After all they
1253 key_tags and stuff are not unique, and
1254 might be shared by multiple keys. */
1255 found_invalid
= true;
1258 case DNSSEC_UNSUPPORTED_ALGORITHM
:
1259 /* If the key algorithm is
1260 unsupported, try another
1261 RRSIG/DNSKEY pair, but remember we
1262 encountered this, so that we can
1263 return a proper error when we
1264 encounter nothing better. */
1265 found_unsupported_algorithm
= true;
1268 case DNSSEC_SIGNATURE_EXPIRED
:
1269 /* If the signature is expired, try
1270 another one, but remember it, so
1271 that we can return this */
1272 found_expired_rrsig
= true;
1276 assert_not_reached();
1281 if (found_expired_rrsig
)
1282 *result
= DNSSEC_SIGNATURE_EXPIRED
;
1283 else if (found_unsupported_algorithm
)
1284 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
1285 else if (found_invalid
)
1286 *result
= DNSSEC_INVALID
;
1287 else if (found_rrsig
)
1288 *result
= DNSSEC_MISSING_KEY
;
1290 *result
= DNSSEC_NO_SIGNATURE
;
1295 return (int) nvalidations
;
1298 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
1299 DnsResourceRecord
*rr
;
1302 /* Checks whether there's at least one RRSIG in 'a' that protects RRs of the specified key */
1304 DNS_ANSWER_FOREACH(rr
, a
) {
1305 r
= dnssec_key_match_rrsig(key
, rr
);
1315 static hash_md_t
digest_to_hash_md(uint8_t algorithm
) {
1317 /* Translates a DNSSEC digest algorithm into an openssl/gcrypt digest identifier */
1319 switch (algorithm
) {
1321 case DNSSEC_DIGEST_SHA1
:
1322 return OPENSSL_OR_GCRYPT(EVP_sha1(), GCRY_MD_SHA1
);
1324 case DNSSEC_DIGEST_SHA256
:
1325 return OPENSSL_OR_GCRYPT(EVP_sha256(), GCRY_MD_SHA256
);
1327 case DNSSEC_DIGEST_SHA384
:
1328 return OPENSSL_OR_GCRYPT(EVP_sha384(), GCRY_MD_SHA384
);
1331 return OPENSSL_OR_GCRYPT(NULL
, -EOPNOTSUPP
);
1335 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
1336 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1342 /* Implements DNSKEY verification by a DS, according to RFC 4035, section 5.2 */
1344 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1346 if (ds
->key
->type
!= DNS_TYPE_DS
)
1348 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
1349 return -EKEYREJECTED
;
1350 if (!mask_revoke
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
1351 return -EKEYREJECTED
;
1352 if (dnskey
->dnskey
.protocol
!= 3)
1353 return -EKEYREJECTED
;
1355 if (dnskey
->dnskey
.algorithm
!= ds
->ds
.algorithm
)
1357 if (dnssec_keytag(dnskey
, mask_revoke
) != ds
->ds
.key_tag
)
1360 r
= dns_name_to_wire_format(dns_resource_key_name(dnskey
->key
), wire_format
, sizeof wire_format
, true);
1364 hash_md_t md_algorithm
= digest_to_hash_md(ds
->ds
.digest_type
);
1370 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= NULL
;
1371 uint8_t result
[EVP_MAX_MD_SIZE
];
1373 unsigned hash_size
= EVP_MD_size(md_algorithm
);
1374 assert(hash_size
> 0);
1376 if (ds
->ds
.digest_size
!= hash_size
)
1379 ctx
= EVP_MD_CTX_new();
1383 if (EVP_DigestInit_ex(ctx
, md_algorithm
, NULL
) <= 0)
1386 if (EVP_DigestUpdate(ctx
, wire_format
, r
) <= 0)
1390 md_add_uint16(ctx
, dnskey
->dnskey
.flags
& ~DNSKEY_FLAG_REVOKE
);
1392 md_add_uint16(ctx
, dnskey
->dnskey
.flags
);
1394 r
= md_add_uint8(ctx
, dnskey
->dnskey
.protocol
);
1397 r
= md_add_uint8(ctx
, dnskey
->dnskey
.algorithm
);
1400 if (EVP_DigestUpdate(ctx
, dnskey
->dnskey
.key
, dnskey
->dnskey
.key_size
) <= 0)
1403 if (EVP_DigestFinal_ex(ctx
, result
, NULL
) <= 0)
1407 if (md_algorithm
< 0)
1410 initialize_libgcrypt(false);
1412 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
1414 size_t hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
1415 assert(hash_size
> 0);
1417 if (ds
->ds
.digest_size
!= hash_size
)
1420 gcry_error_t err
= gcry_md_open(&md
, md_algorithm
, 0);
1421 if (gcry_err_code(err
) != GPG_ERR_NO_ERROR
|| !md
)
1424 gcry_md_write(md
, wire_format
, r
);
1426 md_add_uint16(md
, dnskey
->dnskey
.flags
& ~DNSKEY_FLAG_REVOKE
);
1428 md_add_uint16(md
, dnskey
->dnskey
.flags
);
1429 md_add_uint8(md
, dnskey
->dnskey
.protocol
);
1430 md_add_uint8(md
, dnskey
->dnskey
.algorithm
);
1431 gcry_md_write(md
, dnskey
->dnskey
.key
, dnskey
->dnskey
.key_size
);
1433 void *result
= gcry_md_read(md
, 0);
1438 return memcmp(result
, ds
->ds
.digest
, ds
->ds
.digest_size
) == 0;
1441 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
1442 DnsResourceRecord
*ds
;
1443 DnsAnswerFlags flags
;
1448 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1451 DNS_ANSWER_FOREACH_FLAGS(ds
, flags
, validated_ds
) {
1453 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1456 if (ds
->key
->type
!= DNS_TYPE_DS
)
1458 if (ds
->key
->class != dnskey
->key
->class)
1461 r
= dns_name_equal(dns_resource_key_name(dnskey
->key
), dns_resource_key_name(ds
->key
));
1467 r
= dnssec_verify_dnskey_by_ds(dnskey
, ds
, false);
1468 if (IN_SET(r
, -EKEYREJECTED
, -EOPNOTSUPP
))
1469 return 0; /* The DNSKEY is revoked or otherwise invalid, or we don't support the digest algorithm */
1479 static hash_md_t
nsec3_hash_to_hash_md(uint8_t algorithm
) {
1481 /* Translates a DNSSEC NSEC3 hash algorithm into an openssl/gcrypt digest identifier */
1483 switch (algorithm
) {
1485 case NSEC3_ALGORITHM_SHA1
:
1486 return OPENSSL_OR_GCRYPT(EVP_sha1(), GCRY_MD_SHA1
);
1489 return OPENSSL_OR_GCRYPT(NULL
, -EOPNOTSUPP
);
1493 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
1494 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1501 if (nsec3
->key
->type
!= DNS_TYPE_NSEC3
)
1504 if (nsec3
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1505 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1506 "Ignoring NSEC3 RR %s with excessive number of iterations.",
1507 dns_resource_record_to_string(nsec3
));
1509 hash_md_t algorithm
= nsec3_hash_to_hash_md(nsec3
->nsec3
.algorithm
);
1514 size_t hash_size
= EVP_MD_size(algorithm
);
1515 assert(hash_size
> 0);
1517 if (nsec3
->nsec3
.next_hashed_name_size
!= hash_size
)
1520 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
1524 if (EVP_DigestInit_ex(ctx
, algorithm
, NULL
) <= 0)
1527 r
= dns_name_to_wire_format(name
, wire_format
, sizeof(wire_format
), true);
1531 if (EVP_DigestUpdate(ctx
, wire_format
, r
) <= 0)
1533 if (EVP_DigestUpdate(ctx
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
) <= 0)
1536 uint8_t result
[EVP_MAX_MD_SIZE
];
1537 if (EVP_DigestFinal_ex(ctx
, result
, NULL
) <= 0)
1540 for (unsigned k
= 0; k
< nsec3
->nsec3
.iterations
; k
++) {
1541 if (EVP_DigestInit_ex(ctx
, algorithm
, NULL
) <= 0)
1543 if (EVP_DigestUpdate(ctx
, result
, hash_size
) <= 0)
1545 if (EVP_DigestUpdate(ctx
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
) <= 0)
1548 if (EVP_DigestFinal_ex(ctx
, result
, NULL
) <= 0)
1555 initialize_libgcrypt(false);
1557 unsigned hash_size
= gcry_md_get_algo_dlen(algorithm
);
1558 assert(hash_size
> 0);
1560 if (nsec3
->nsec3
.next_hashed_name_size
!= hash_size
)
1563 r
= dns_name_to_wire_format(name
, wire_format
, sizeof(wire_format
), true);
1567 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
1568 gcry_error_t err
= gcry_md_open(&md
, algorithm
, 0);
1569 if (gcry_err_code(err
) != GPG_ERR_NO_ERROR
|| !md
)
1572 gcry_md_write(md
, wire_format
, r
);
1573 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1575 void *result
= gcry_md_read(md
, 0);
1579 for (unsigned k
= 0; k
< nsec3
->nsec3
.iterations
; k
++) {
1580 uint8_t tmp
[hash_size
];
1581 memcpy(tmp
, result
, hash_size
);
1584 gcry_md_write(md
, tmp
, hash_size
);
1585 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1587 result
= gcry_md_read(md
, 0);
1593 memcpy(ret
, result
, hash_size
);
1594 return (int) hash_size
;
1597 static int nsec3_is_good(DnsResourceRecord
*rr
, DnsResourceRecord
*nsec3
) {
1603 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
1606 /* RFC 5155, Section 8.2 says we MUST ignore NSEC3 RRs with flags != 0 or 1 */
1607 if (!IN_SET(rr
->nsec3
.flags
, 0, 1))
1610 /* Ignore NSEC3 RRs whose algorithm we don't know */
1612 if (!nsec3_hash_to_hash_md(rr
->nsec3
.algorithm
))
1615 if (nsec3_hash_to_hash_md(rr
->nsec3
.algorithm
) < 0)
1619 /* Ignore NSEC3 RRs with an excessive number of required iterations */
1620 if (rr
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1623 /* Ignore NSEC3 RRs generated from wildcards. If these NSEC3 RRs weren't correctly signed we can't make this
1624 * check (since rr->n_skip_labels_source is -1), but that's OK, as we won't trust them anyway in that case. */
1625 if (!IN_SET(rr
->n_skip_labels_source
, 0, UINT8_MAX
))
1627 /* Ignore NSEC3 RRs that are located anywhere else than one label below the zone */
1628 if (!IN_SET(rr
->n_skip_labels_signer
, 1, UINT8_MAX
))
1634 /* If a second NSEC3 RR is specified, also check if they are from the same zone. */
1636 if (nsec3
== rr
) /* Shortcut */
1639 if (rr
->key
->class != nsec3
->key
->class)
1641 if (rr
->nsec3
.algorithm
!= nsec3
->nsec3
.algorithm
)
1643 if (rr
->nsec3
.iterations
!= nsec3
->nsec3
.iterations
)
1645 if (rr
->nsec3
.salt_size
!= nsec3
->nsec3
.salt_size
)
1647 if (memcmp_safe(rr
->nsec3
.salt
, nsec3
->nsec3
.salt
, rr
->nsec3
.salt_size
) != 0)
1650 a
= dns_resource_key_name(rr
->key
);
1651 r
= dns_name_parent(&a
); /* strip off hash */
1655 b
= dns_resource_key_name(nsec3
->key
);
1656 r
= dns_name_parent(&b
); /* strip off hash */
1660 /* Make sure both have the same parent */
1661 return dns_name_equal(a
, b
);
1664 static int nsec3_hashed_domain_format(const uint8_t *hashed
, size_t hashed_size
, const char *zone
, char **ret
) {
1665 _cleanup_free_
char *l
= NULL
;
1669 assert(hashed_size
> 0);
1673 l
= base32hexmem(hashed
, hashed_size
, false);
1677 j
= strjoin(l
, ".", zone
);
1682 return (int) hashed_size
;
1685 static int nsec3_hashed_domain_make(DnsResourceRecord
*nsec3
, const char *domain
, const char *zone
, char **ret
) {
1686 uint8_t hashed
[DNSSEC_HASH_SIZE_MAX
];
1694 hashed_size
= dnssec_nsec3_hash(nsec3
, domain
, hashed
);
1695 if (hashed_size
< 0)
1698 return nsec3_hashed_domain_format(hashed
, (size_t) hashed_size
, zone
, ret
);
1701 /* See RFC 5155, Section 8
1702 * First try to find a NSEC3 record that matches our query precisely, if that fails, find the closest
1703 * enclosure. Secondly, find a proof that there is no closer enclosure and either a proof that there
1704 * is no wildcard domain as a direct descendant of the closest enclosure, or find an NSEC3 record that
1705 * matches the wildcard domain.
1707 * Based on this we can prove either the existence of the record in @key, or NXDOMAIN or NODATA, or
1708 * that there is no proof either way. The latter is the case if a proof of non-existence of a given
1709 * name uses an NSEC3 record with the opt-out bit set. Lastly, if we are given insufficient NSEC3 records
1710 * to conclude anything we indicate this by returning NO_RR. */
1711 static int dnssec_test_nsec3(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1712 _cleanup_free_
char *next_closer_domain
= NULL
, *wildcard_domain
= NULL
;
1713 const char *zone
, *p
, *pp
= NULL
, *wildcard
;
1714 DnsResourceRecord
*rr
, *enclosure_rr
, *zone_rr
, *wildcard_rr
= NULL
;
1715 DnsAnswerFlags flags
;
1717 bool a
, no_closer
= false, no_wildcard
= false, optout
= false;
1722 /* First step, find the zone name and the NSEC3 parameters of the zone.
1723 * it is sufficient to look for the longest common suffix we find with
1724 * any NSEC3 RR in the response. Any NSEC3 record will do as all NSEC3
1725 * records from a given zone in a response must use the same
1727 zone
= dns_resource_key_name(key
);
1729 DNS_ANSWER_FOREACH_FLAGS(zone_rr
, flags
, answer
) {
1730 r
= nsec3_is_good(zone_rr
, NULL
);
1736 r
= dns_name_equal_skip(dns_resource_key_name(zone_rr
->key
), 1, zone
);
1743 /* Strip one label from the front */
1744 r
= dns_name_parent(&zone
);
1751 *result
= DNSSEC_NSEC_NO_RR
;
1755 /* Second step, find the closest encloser NSEC3 RR in 'answer' that matches 'key' */
1756 p
= dns_resource_key_name(key
);
1758 _cleanup_free_
char *hashed_domain
= NULL
;
1760 hashed_size
= nsec3_hashed_domain_make(zone_rr
, p
, zone
, &hashed_domain
);
1761 if (hashed_size
== -EOPNOTSUPP
) {
1762 *result
= DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
;
1765 if (hashed_size
< 0)
1768 DNS_ANSWER_FOREACH_FLAGS(enclosure_rr
, flags
, answer
) {
1770 r
= nsec3_is_good(enclosure_rr
, zone_rr
);
1776 if (enclosure_rr
->nsec3
.next_hashed_name_size
!= (size_t) hashed_size
)
1779 r
= dns_name_equal(dns_resource_key_name(enclosure_rr
->key
), hashed_domain
);
1783 a
= flags
& DNS_ANSWER_AUTHENTICATED
;
1784 goto found_closest_encloser
;
1788 /* We didn't find the closest encloser with this name,
1789 * but let's remember this domain name, it might be
1790 * the next closer name */
1794 /* Strip one label from the front */
1795 r
= dns_name_parent(&p
);
1802 *result
= DNSSEC_NSEC_NO_RR
;
1805 found_closest_encloser
:
1806 /* We found a closest encloser in 'p'; next closer is 'pp' */
1809 /* We have an exact match! If we area looking for a DS RR, then we must insist that we got the NSEC3 RR
1810 * from the parent. Otherwise the one from the child. Do so, by checking whether SOA and NS are
1811 * appropriately set. */
1813 if (key
->type
== DNS_TYPE_DS
) {
1814 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1817 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1818 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1822 /* No next closer NSEC3 RR. That means there's a direct NSEC3 RR for our key. */
1823 if (bitmap_isset(enclosure_rr
->nsec3
.types
, key
->type
))
1824 *result
= DNSSEC_NSEC_FOUND
;
1825 else if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1826 *result
= DNSSEC_NSEC_CNAME
;
1828 *result
= DNSSEC_NSEC_NODATA
;
1833 *ttl
= enclosure_rr
->ttl
;
1838 /* Ensure this is not a DNAME domain, see RFC5155, section 8.3. */
1839 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_DNAME
))
1842 /* Ensure that this data is from the delegated domain
1843 * (i.e. originates from the "lower" DNS server), and isn't
1844 * just glue records (i.e. doesn't originate from the "upper"
1846 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1847 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1850 /* Prove that there is no next closer and whether or not there is a wildcard domain. */
1852 wildcard
= strjoina("*.", p
);
1853 r
= nsec3_hashed_domain_make(enclosure_rr
, wildcard
, zone
, &wildcard_domain
);
1856 if (r
!= hashed_size
)
1859 r
= nsec3_hashed_domain_make(enclosure_rr
, pp
, zone
, &next_closer_domain
);
1862 if (r
!= hashed_size
)
1865 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1866 _cleanup_free_
char *next_hashed_domain
= NULL
;
1868 r
= nsec3_is_good(rr
, zone_rr
);
1874 r
= nsec3_hashed_domain_format(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, zone
, &next_hashed_domain
);
1878 r
= dns_name_between(dns_resource_key_name(rr
->key
), next_closer_domain
, next_hashed_domain
);
1882 if (rr
->nsec3
.flags
& 1)
1885 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1890 r
= dns_name_equal(dns_resource_key_name(rr
->key
), wildcard_domain
);
1894 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1899 r
= dns_name_between(dns_resource_key_name(rr
->key
), wildcard_domain
, next_hashed_domain
);
1903 if (rr
->nsec3
.flags
& 1)
1904 /* This only makes sense if we have a wildcard delegation, which is
1905 * very unlikely, see RFC 4592, Section 4.2, but we cannot rely on
1906 * this not happening, so hence cannot simply conclude NXDOMAIN as
1910 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1916 if (wildcard_rr
&& no_wildcard
)
1920 *result
= DNSSEC_NSEC_NO_RR
;
1925 /* A wildcard exists that matches our query. */
1927 /* This is not specified in any RFC to the best of my knowledge, but
1928 * if the next closer enclosure is covered by an opt-out NSEC3 RR
1929 * it means that we cannot prove that the source of synthesis is
1930 * correct, as there may be a closer match. */
1931 *result
= DNSSEC_NSEC_OPTOUT
;
1932 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, key
->type
))
1933 *result
= DNSSEC_NSEC_FOUND
;
1934 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1935 *result
= DNSSEC_NSEC_CNAME
;
1937 *result
= DNSSEC_NSEC_NODATA
;
1940 /* The RFC only specifies that we have to care for optout for NODATA for
1941 * DS records. However, children of an insecure opt-out delegation should
1942 * also be considered opt-out, rather than verified NXDOMAIN.
1943 * Note that we do not require a proof of wildcard non-existence if the
1944 * next closer domain is covered by an opt-out, as that would not provide
1945 * any additional information. */
1946 *result
= DNSSEC_NSEC_OPTOUT
;
1947 else if (no_wildcard
)
1948 *result
= DNSSEC_NSEC_NXDOMAIN
;
1950 *result
= DNSSEC_NSEC_NO_RR
;
1960 *ttl
= enclosure_rr
->ttl
;
1965 static int dnssec_nsec_wildcard_equal(DnsResourceRecord
*rr
, const char *name
) {
1966 char label
[DNS_LABEL_MAX
];
1971 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1973 /* Checks whether the specified RR has a name beginning in "*.", and if the rest is a suffix of our name */
1975 if (rr
->n_skip_labels_source
!= 1)
1978 n
= dns_resource_key_name(rr
->key
);
1979 r
= dns_label_unescape(&n
, label
, sizeof label
, 0);
1982 if (r
!= 1 || label
[0] != '*')
1985 return dns_name_endswith(name
, n
);
1988 static int dnssec_nsec_in_path(DnsResourceRecord
*rr
, const char *name
) {
1989 const char *nn
, *common_suffix
;
1993 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1995 /* Checks whether the specified nsec RR indicates that name is an empty non-terminal (ENT)
1997 * A couple of examples:
1999 * NSEC bar → waldo.foo.bar: indicates that foo.bar exists and is an ENT
2000 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that xoo.bar and zzz.xoo.bar exist and are ENTs
2001 * NSEC yyy.zzz.xoo.bar → bar: indicates pretty much nothing about ENTs
2004 /* First, determine parent of next domain. */
2005 nn
= rr
->nsec
.next_domain_name
;
2006 r
= dns_name_parent(&nn
);
2010 /* If the name we just determined is not equal or child of the name we are interested in, then we can't say
2011 * anything at all. */
2012 r
= dns_name_endswith(nn
, name
);
2016 /* 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. */
2017 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
2021 return dns_name_endswith(name
, common_suffix
);
2024 static int dnssec_nsec_from_parent_zone(DnsResourceRecord
*rr
, const char *name
) {
2028 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
2030 /* Checks whether this NSEC originates to the parent zone or the child zone. */
2032 r
= dns_name_parent(&name
);
2036 r
= dns_name_equal(name
, dns_resource_key_name(rr
->key
));
2040 /* DNAME, and NS without SOA is an indication for a delegation. */
2041 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_DNAME
))
2044 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) && !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
2050 static int dnssec_nsec_covers(DnsResourceRecord
*rr
, const char *name
) {
2055 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
2057 /* Checks whether the name is covered by this NSEC RR. This means, that the name is somewhere below the NSEC's
2058 * signer name, and between the NSEC's two names. */
2060 r
= dns_resource_record_signer(rr
, &signer
);
2064 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
2068 return dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
2071 static int dnssec_nsec_generate_wildcard(DnsResourceRecord
*rr
, const char *name
, char **wc
) {
2072 const char *common_suffix1
, *common_suffix2
, *signer
;
2073 int r
, labels1
, labels2
;
2076 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
2078 /* Generates "Wildcard at the Closest Encloser" for the given name and NSEC RR. */
2080 r
= dns_resource_record_signer(rr
, &signer
);
2084 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
2088 r
= dns_name_common_suffix(name
, dns_resource_key_name(rr
->key
), &common_suffix1
);
2092 r
= dns_name_common_suffix(name
, rr
->nsec
.next_domain_name
, &common_suffix2
);
2096 labels1
= dns_name_count_labels(common_suffix1
);
2100 labels2
= dns_name_count_labels(common_suffix2
);
2104 if (labels1
> labels2
)
2105 r
= dns_name_concat("*", common_suffix1
, 0, wc
);
2107 r
= dns_name_concat("*", common_suffix2
, 0, wc
);
2115 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
2116 bool have_nsec3
= false, covering_rr_authenticated
= false, wildcard_rr_authenticated
= false;
2117 DnsResourceRecord
*rr
, *covering_rr
= NULL
, *wildcard_rr
= NULL
;
2118 DnsAnswerFlags flags
;
2125 /* Look for any NSEC/NSEC3 RRs that say something about the specified key. */
2127 name
= dns_resource_key_name(key
);
2129 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
2131 if (rr
->key
->class != key
->class)
2134 have_nsec3
= have_nsec3
|| (rr
->key
->type
== DNS_TYPE_NSEC3
);
2136 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
2139 /* The following checks only make sense for NSEC RRs that are not expanded from a wildcard */
2140 r
= dns_resource_record_is_synthetic(rr
);
2141 if (r
== -ENODATA
) /* No signing RR known. */
2148 /* Check if this is a direct match. If so, we have encountered a NODATA case */
2149 r
= dns_name_equal(dns_resource_key_name(rr
->key
), name
);
2153 /* If it's not a direct match, maybe it's a wild card match? */
2154 r
= dnssec_nsec_wildcard_equal(rr
, name
);
2159 if (key
->type
== DNS_TYPE_DS
) {
2160 /* If we look for a DS RR and the server sent us the NSEC RR of the child zone
2161 * we have a problem. For DS RRs we want the NSEC RR from the parent */
2162 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
2165 /* For all RR types, ensure that if NS is set SOA is set too, so that we know
2166 * we got the child's NSEC. */
2167 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) &&
2168 !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
2172 if (bitmap_isset(rr
->nsec
.types
, key
->type
))
2173 *result
= DNSSEC_NSEC_FOUND
;
2174 else if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_CNAME
))
2175 *result
= DNSSEC_NSEC_CNAME
;
2177 *result
= DNSSEC_NSEC_NODATA
;
2180 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2187 /* Check if the name we are looking for is an empty non-terminal within the owner or next name
2188 * of the NSEC RR. */
2189 r
= dnssec_nsec_in_path(rr
, name
);
2193 *result
= DNSSEC_NSEC_NODATA
;
2196 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2203 /* The following two "covering" checks, are not useful if the NSEC is from the parent */
2204 r
= dnssec_nsec_from_parent_zone(rr
, name
);
2210 /* Check if this NSEC RR proves the absence of an explicit RR under this name */
2211 r
= dnssec_nsec_covers(rr
, name
);
2214 if (r
> 0 && (!covering_rr
|| !covering_rr_authenticated
)) {
2216 covering_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2221 _cleanup_free_
char *wc
= NULL
;
2222 r
= dnssec_nsec_generate_wildcard(covering_rr
, name
, &wc
);
2226 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
2228 if (rr
->key
->class != key
->class)
2231 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
2234 /* Check if this NSEC RR proves the nonexistence of the wildcard */
2235 r
= dnssec_nsec_covers(rr
, wc
);
2238 if (r
> 0 && (!wildcard_rr
|| !wildcard_rr_authenticated
)) {
2240 wildcard_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2245 if (covering_rr
&& wildcard_rr
) {
2246 /* If we could prove that neither the name itself, nor the wildcard at the closest encloser exists, we
2247 * proved the NXDOMAIN case. */
2248 *result
= DNSSEC_NSEC_NXDOMAIN
;
2251 *authenticated
= covering_rr_authenticated
&& wildcard_rr_authenticated
;
2253 *ttl
= MIN(covering_rr
->ttl
, wildcard_rr
->ttl
);
2258 /* OK, this was not sufficient. Let's see if NSEC3 can help. */
2260 return dnssec_test_nsec3(answer
, key
, result
, authenticated
, ttl
);
2262 /* No appropriate NSEC RR found, report this. */
2263 *result
= DNSSEC_NSEC_NO_RR
;
2267 static int dnssec_nsec_test_enclosed(DnsAnswer
*answer
, uint16_t type
, const char *name
, const char *zone
, bool *authenticated
) {
2268 DnsResourceRecord
*rr
;
2269 DnsAnswerFlags flags
;
2275 /* Checks whether there's an NSEC/NSEC3 that proves that the specified 'name' is non-existing in the specified
2276 * 'zone'. The 'zone' must be a suffix of the 'name'. */
2278 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
2281 if (rr
->key
->type
!= type
&& type
!= DNS_TYPE_ANY
)
2284 switch (rr
->key
->type
) {
2288 /* We only care for NSEC RRs from the indicated zone */
2289 r
= dns_resource_record_is_signer(rr
, zone
);
2295 r
= dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
2302 case DNS_TYPE_NSEC3
: {
2303 _cleanup_free_
char *hashed_domain
= NULL
, *next_hashed_domain
= NULL
;
2305 /* We only care for NSEC3 RRs from the indicated zone */
2306 r
= dns_resource_record_is_signer(rr
, zone
);
2312 r
= nsec3_is_good(rr
, NULL
);
2318 /* Format the domain we are testing with the NSEC3 RR's hash function */
2319 r
= nsec3_hashed_domain_make(
2326 if ((size_t) r
!= rr
->nsec3
.next_hashed_name_size
)
2329 /* Format the NSEC3's next hashed name as proper domain name */
2330 r
= nsec3_hashed_domain_format(
2331 rr
->nsec3
.next_hashed_name
,
2332 rr
->nsec3
.next_hashed_name_size
,
2334 &next_hashed_domain
);
2338 r
= dns_name_between(dns_resource_key_name(rr
->key
), hashed_domain
, next_hashed_domain
);
2352 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2360 static int dnssec_test_positive_wildcard_nsec3(
2365 bool *authenticated
) {
2367 const char *next_closer
= NULL
;
2370 /* Run a positive NSEC3 wildcard proof. Specifically:
2372 * A proof that the "next closer" of the generating wildcard does not exist.
2374 * Note a key difference between the NSEC3 and NSEC versions of the proof. NSEC RRs don't have to exist for
2375 * empty non-transients. NSEC3 RRs however have to. This means it's sufficient to check if the next closer name
2376 * exists for the NSEC3 RR and we are done.
2378 * 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
2379 * c.d.e.f does not exist. */
2383 r
= dns_name_parent(&name
);
2387 r
= dns_name_equal(name
, source
);
2394 return dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC3
, next_closer
, zone
, authenticated
);
2397 static int dnssec_test_positive_wildcard_nsec(
2402 bool *_authenticated
) {
2404 bool authenticated
= true;
2407 /* Run a positive NSEC wildcard proof. Specifically:
2409 * A proof that there's neither a wildcard name nor a non-wildcard name that is a suffix of the name "name" and
2410 * a prefix of the synthesizing source "source" in the zone "zone".
2412 * See RFC 5155, Section 8.8 and RFC 4035, Section 5.3.4
2414 * 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
2415 * have to prove that none of the following exist:
2425 _cleanup_free_
char *wc
= NULL
;
2428 /* Check if there's an NSEC or NSEC3 RR that proves that the mame we determined is really non-existing,
2429 * i.e between the owner name and the next name of an NSEC RR. */
2430 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, name
, zone
, &a
);
2434 authenticated
= authenticated
&& a
;
2436 /* Strip one label off */
2437 r
= dns_name_parent(&name
);
2441 /* Did we reach the source of synthesis? */
2442 r
= dns_name_equal(name
, source
);
2446 /* Successful exit */
2447 *_authenticated
= authenticated
;
2451 /* Safety check, that the source of synthesis is still our suffix */
2452 r
= dns_name_endswith(name
, source
);
2458 /* Replace the label we stripped off with an asterisk */
2459 wc
= strjoin("*.", name
);
2463 /* And check if the proof holds for the asterisk name, too */
2464 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, wc
, zone
, &a
);
2468 authenticated
= authenticated
&& a
;
2469 /* In the next iteration we'll check the non-asterisk-prefixed version */
2473 int dnssec_test_positive_wildcard(
2478 bool *authenticated
) {
2485 assert(authenticated
);
2487 r
= dns_answer_contains_zone_nsec3(answer
, zone
);
2491 return dnssec_test_positive_wildcard_nsec3(answer
, name
, source
, zone
, authenticated
);
2493 return dnssec_test_positive_wildcard_nsec(answer
, name
, source
, zone
, authenticated
);
2498 int dnssec_verify_rrset(
2500 const DnsResourceKey
*key
,
2501 DnsResourceRecord
*rrsig
,
2502 DnsResourceRecord
*dnskey
,
2504 DnssecResult
*result
) {
2509 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
2514 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
2519 int dnssec_verify_rrset_search(
2521 const DnsResourceKey
*key
,
2522 DnsAnswer
*validated_dnskeys
,
2524 DnssecResult
*result
,
2525 DnsResourceRecord
**ret_rrsig
) {
2530 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
2535 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
2540 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
2545 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
2550 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
2555 int dnssec_test_positive_wildcard(
2560 bool *authenticated
) {
2567 static const char* const dnssec_result_table
[_DNSSEC_RESULT_MAX
] = {
2568 [DNSSEC_VALIDATED
] = "validated",
2569 [DNSSEC_VALIDATED_WILDCARD
] = "validated-wildcard",
2570 [DNSSEC_INVALID
] = "invalid",
2571 [DNSSEC_SIGNATURE_EXPIRED
] = "signature-expired",
2572 [DNSSEC_UNSUPPORTED_ALGORITHM
] = "unsupported-algorithm",
2573 [DNSSEC_NO_SIGNATURE
] = "no-signature",
2574 [DNSSEC_MISSING_KEY
] = "missing-key",
2575 [DNSSEC_UNSIGNED
] = "unsigned",
2576 [DNSSEC_FAILED_AUXILIARY
] = "failed-auxiliary",
2577 [DNSSEC_NSEC_MISMATCH
] = "nsec-mismatch",
2578 [DNSSEC_INCOMPATIBLE_SERVER
] = "incompatible-server",
2579 [DNSSEC_TOO_MANY_VALIDATIONS
] = "too-many-validations",
2581 DEFINE_STRING_TABLE_LOOKUP(dnssec_result
, DnssecResult
);
2583 static const char* const dnssec_verdict_table
[_DNSSEC_VERDICT_MAX
] = {
2584 [DNSSEC_SECURE
] = "secure",
2585 [DNSSEC_INSECURE
] = "insecure",
2586 [DNSSEC_BOGUS
] = "bogus",
2587 [DNSSEC_INDETERMINATE
] = "indeterminate",
2589 DEFINE_STRING_TABLE_LOOKUP(dnssec_verdict
, DnssecVerdict
);