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 "openssl-util.h"
11 #include "resolved-dns-dnssec.h"
12 #include "resolved-dns-packet.h"
13 #include "sort-util.h"
14 #include "string-table.h"
17 # pragma GCC diagnostic push
18 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
19 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(RSA
*, RSA_free
, NULL
);
20 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(EC_KEY
*, EC_KEY_free
, NULL
);
21 # pragma GCC diagnostic pop
24 #define VERIFY_RRS_MAX 256
25 #define MAX_KEY_SIZE (32*1024)
27 /* Permit a maximum clock skew of 1h 10min. This should be enough to deal with DST confusion */
28 #define SKEW_MAX (1*USEC_PER_HOUR + 10*USEC_PER_MINUTE)
30 /* Maximum number of NSEC3 iterations we'll do. RFC5155 says 2500 shall be the maximum useful value */
31 #define NSEC3_ITERATIONS_MAX 2500
34 * The DNSSEC Chain of trust:
36 * Normal RRs are protected via RRSIG RRs in combination with DNSKEY RRs, all in the same zone
37 * DNSKEY RRs are either protected like normal RRs, or via a DS from a zone "higher" up the tree
38 * DS RRs are protected like normal RRs
41 * Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS
44 uint16_t dnssec_keytag(DnsResourceRecord
*dnskey
, bool mask_revoke
) {
48 /* The algorithm from RFC 4034, Appendix B. */
51 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
53 f
= (uint32_t) dnskey
->dnskey
.flags
;
56 f
&= ~DNSKEY_FLAG_REVOKE
;
58 sum
= f
+ ((((uint32_t) dnskey
->dnskey
.protocol
) << 8) + (uint32_t) dnskey
->dnskey
.algorithm
);
60 p
= dnskey
->dnskey
.key
;
62 for (size_t i
= 0; i
< dnskey
->dnskey
.key_size
; i
++)
63 sum
+= (i
& 1) == 0 ? (uint32_t) p
[i
] << 8 : (uint32_t) p
[i
];
65 sum
+= (sum
>> 16) & UINT32_C(0xFFFF);
67 return sum
& UINT32_C(0xFFFF);
70 #if HAVE_OPENSSL_OR_GCRYPT
72 static int rr_compare(DnsResourceRecord
* const *a
, DnsResourceRecord
* const *b
) {
73 const DnsResourceRecord
*x
= *a
, *y
= *b
;
77 /* Let's order the RRs according to RFC 4034, Section 6.3 */
80 assert(x
->wire_format
);
82 assert(y
->wire_format
);
84 m
= MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(x
), DNS_RESOURCE_RECORD_RDATA_SIZE(y
));
86 r
= memcmp(DNS_RESOURCE_RECORD_RDATA(x
), DNS_RESOURCE_RECORD_RDATA(y
), m
);
90 return CMP(DNS_RESOURCE_RECORD_RDATA_SIZE(x
), DNS_RESOURCE_RECORD_RDATA_SIZE(y
));
93 static int dnssec_rsa_verify_raw(
94 hash_algorithm_t hash_algorithm
,
95 const void *signature
, size_t signature_size
,
96 const void *data
, size_t data_size
,
97 const void *exponent
, size_t exponent_size
,
98 const void *modulus
, size_t modulus_size
) {
102 # pragma GCC diagnostic push
103 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
104 _cleanup_(RSA_freep
) RSA
*rpubkey
= NULL
;
105 _cleanup_(EVP_PKEY_freep
) EVP_PKEY
*epubkey
= NULL
;
106 _cleanup_(EVP_PKEY_CTX_freep
) EVP_PKEY_CTX
*ctx
= NULL
;
107 _cleanup_(BN_freep
) BIGNUM
*e
= NULL
, *m
= NULL
;
109 assert(hash_algorithm
);
111 e
= BN_bin2bn(exponent
, exponent_size
, NULL
);
115 m
= BN_bin2bn(modulus
, modulus_size
, NULL
);
123 if (RSA_set0_key(rpubkey
, m
, e
, NULL
) <= 0)
127 assert((size_t) RSA_size(rpubkey
) == signature_size
);
129 epubkey
= EVP_PKEY_new();
133 if (EVP_PKEY_assign_RSA(epubkey
, RSAPublicKey_dup(rpubkey
)) <= 0)
136 ctx
= EVP_PKEY_CTX_new(epubkey
, NULL
);
140 if (EVP_PKEY_verify_init(ctx
) <= 0)
143 if (EVP_PKEY_CTX_set_rsa_padding(ctx
, RSA_PKCS1_PADDING
) <= 0)
146 if (EVP_PKEY_CTX_set_signature_md(ctx
, hash_algorithm
) <= 0)
149 r
= EVP_PKEY_verify(ctx
, signature
, signature_size
, data
, data_size
);
151 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
152 "Signature verification failed: 0x%lx", ERR_get_error());
154 # pragma GCC diagnostic pop
156 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
157 gcry_mpi_t n
= NULL
, e
= NULL
, s
= NULL
;
160 assert(hash_algorithm
);
162 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature
, signature_size
, NULL
);
168 ge
= gcry_mpi_scan(&e
, GCRYMPI_FMT_USG
, exponent
, exponent_size
, NULL
);
174 ge
= gcry_mpi_scan(&n
, GCRYMPI_FMT_USG
, modulus
, modulus_size
, NULL
);
180 ge
= gcry_sexp_build(&signature_sexp
,
182 "(sig-val (rsa (s %m)))",
190 ge
= gcry_sexp_build(&data_sexp
,
192 "(data (flags pkcs1) (hash %s %b))",
201 ge
= gcry_sexp_build(&public_key_sexp
,
203 "(public-key (rsa (n %m) (e %m)))",
211 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
212 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
215 r
= log_debug_errno(SYNTHETIC_ERRNO(EIO
),
216 "RSA signature check failed: %s", gpg_strerror(ge
));
229 gcry_sexp_release(public_key_sexp
);
231 gcry_sexp_release(signature_sexp
);
233 gcry_sexp_release(data_sexp
);
238 static int dnssec_rsa_verify(
239 hash_algorithm_t hash_algorithm
,
240 const void *hash
, size_t hash_size
,
241 DnsResourceRecord
*rrsig
,
242 DnsResourceRecord
*dnskey
) {
244 size_t exponent_size
, modulus_size
;
245 void *exponent
, *modulus
;
247 assert(hash_algorithm
);
249 assert(hash_size
> 0);
253 if (*(uint8_t*) dnskey
->dnskey
.key
== 0) {
254 /* exponent is > 255 bytes long */
256 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 3;
258 ((size_t) (((uint8_t*) dnskey
->dnskey
.key
)[1]) << 8) |
259 ((size_t) ((uint8_t*) dnskey
->dnskey
.key
)[2]);
261 if (exponent_size
< 256)
264 if (3 + exponent_size
>= dnskey
->dnskey
.key_size
)
267 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 3 + exponent_size
;
268 modulus_size
= dnskey
->dnskey
.key_size
- 3 - exponent_size
;
271 /* exponent is <= 255 bytes long */
273 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 1;
274 exponent_size
= (size_t) ((uint8_t*) dnskey
->dnskey
.key
)[0];
276 if (exponent_size
<= 0)
279 if (1 + exponent_size
>= dnskey
->dnskey
.key_size
)
282 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 1 + exponent_size
;
283 modulus_size
= dnskey
->dnskey
.key_size
- 1 - exponent_size
;
286 return dnssec_rsa_verify_raw(
288 rrsig
->rrsig
.signature
, rrsig
->rrsig
.signature_size
,
290 exponent
, exponent_size
,
291 modulus
, modulus_size
);
294 static int dnssec_ecdsa_verify_raw(
295 hash_algorithm_t hash_algorithm
,
296 elliptic_curve_t curve
,
297 const void *signature_r
, size_t signature_r_size
,
298 const void *signature_s
, size_t signature_s_size
,
299 const void *data
, size_t data_size
,
300 const void *key
, size_t key_size
) {
304 # pragma GCC diagnostic push
305 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
306 _cleanup_(EC_GROUP_freep
) EC_GROUP
*ec_group
= NULL
;
307 _cleanup_(EC_POINT_freep
) EC_POINT
*p
= NULL
;
308 _cleanup_(EC_KEY_freep
) EC_KEY
*eckey
= NULL
;
309 _cleanup_(BN_CTX_freep
) BN_CTX
*bctx
= NULL
;
310 _cleanup_(BN_freep
) BIGNUM
*r
= NULL
, *s
= NULL
;
311 _cleanup_(ECDSA_SIG_freep
) ECDSA_SIG
*sig
= NULL
;
313 assert(hash_algorithm
);
315 ec_group
= EC_GROUP_new_by_curve_name(curve
);
319 p
= EC_POINT_new(ec_group
);
327 if (EC_POINT_oct2point(ec_group
, p
, key
, key_size
, bctx
) <= 0)
330 eckey
= EC_KEY_new();
334 if (EC_KEY_set_group(eckey
, ec_group
) <= 0)
337 if (EC_KEY_set_public_key(eckey
, p
) <= 0)
338 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
339 "EC_POINT_bn2point failed: 0x%lx", ERR_get_error());
341 assert(EC_KEY_check_key(eckey
) == 1);
343 r
= BN_bin2bn(signature_r
, signature_r_size
, NULL
);
347 s
= BN_bin2bn(signature_s
, signature_s_size
, NULL
);
351 /* TODO: We should eventually use the EVP API once it supports ECDSA signature verification */
353 sig
= ECDSA_SIG_new();
357 if (ECDSA_SIG_set0(sig
, r
, s
) <= 0)
361 k
= ECDSA_do_verify(data
, data_size
, sig
, eckey
);
363 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
364 "Signature verification failed: 0x%lx", ERR_get_error());
366 # pragma GCC diagnostic pop
368 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
369 gcry_mpi_t q
= NULL
, r
= NULL
, s
= NULL
;
372 assert(hash_algorithm
);
374 ge
= gcry_mpi_scan(&r
, GCRYMPI_FMT_USG
, signature_r
, signature_r_size
, NULL
);
380 ge
= gcry_mpi_scan(&s
, GCRYMPI_FMT_USG
, signature_s
, signature_s_size
, NULL
);
386 ge
= gcry_mpi_scan(&q
, GCRYMPI_FMT_USG
, key
, key_size
, NULL
);
392 ge
= gcry_sexp_build(&signature_sexp
,
394 "(sig-val (ecdsa (r %m) (s %m)))",
402 ge
= gcry_sexp_build(&data_sexp
,
404 "(data (flags rfc6979) (hash %s %b))",
413 ge
= gcry_sexp_build(&public_key_sexp
,
415 "(public-key (ecc (curve %s) (q %m)))",
423 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
424 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
427 log_debug("ECDSA signature check failed: %s", gpg_strerror(ge
));
440 gcry_sexp_release(public_key_sexp
);
442 gcry_sexp_release(signature_sexp
);
444 gcry_sexp_release(data_sexp
);
449 static int dnssec_ecdsa_verify(
450 hash_algorithm_t hash_algorithm
,
452 const void *hash
, size_t hash_size
,
453 DnsResourceRecord
*rrsig
,
454 DnsResourceRecord
*dnskey
) {
456 elliptic_curve_t curve
;
465 if (algorithm
== DNSSEC_ALGORITHM_ECDSAP256SHA256
) {
466 curve
= OPENSSL_OR_GCRYPT(NID_X9_62_prime256v1
, "NIST P-256"); /* NIST P-256 */
468 } else if (algorithm
== DNSSEC_ALGORITHM_ECDSAP384SHA384
) {
469 curve
= OPENSSL_OR_GCRYPT(NID_secp384r1
, "NIST P-384"); /* NIST P-384 */
474 if (dnskey
->dnskey
.key_size
!= key_size
* 2)
477 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
480 q
= newa(uint8_t, key_size
*2 + 1);
481 q
[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
482 memcpy(q
+1, dnskey
->dnskey
.key
, key_size
*2);
484 return dnssec_ecdsa_verify_raw(
487 rrsig
->rrsig
.signature
, key_size
,
488 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
493 static int dnssec_eddsa_verify_raw(
494 elliptic_curve_t curve
,
495 const uint8_t *signature
, size_t signature_size
,
496 const uint8_t *data
, size_t data_size
,
497 const uint8_t *key
, size_t key_size
) {
500 _cleanup_(EVP_PKEY_freep
) EVP_PKEY
*evkey
= NULL
;
501 _cleanup_(EVP_PKEY_CTX_freep
) EVP_PKEY_CTX
*pctx
= NULL
;
502 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= NULL
;
505 assert(curve
== NID_ED25519
);
506 assert(signature_size
== key_size
* 2);
508 uint8_t *q
= newa(uint8_t, signature_size
+ 1);
509 q
[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
510 memcpy(q
+1, signature
, signature_size
);
512 evkey
= EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519
, NULL
, key
, key_size
);
514 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
515 "EVP_PKEY_new_raw_public_key failed: 0x%lx", ERR_get_error());
517 pctx
= EVP_PKEY_CTX_new(evkey
, NULL
);
521 ctx
= EVP_MD_CTX_new();
525 /* This prevents EVP_DigestVerifyInit from managing pctx and complicating our free logic. */
526 EVP_MD_CTX_set_pkey_ctx(ctx
, pctx
);
528 /* One might be tempted to use EVP_PKEY_verify_init, but see Ed25519(7ssl). */
529 if (EVP_DigestVerifyInit(ctx
, &pctx
, NULL
, NULL
, evkey
) <= 0)
532 r
= EVP_DigestVerify(ctx
, signature
, signature_size
, data
, data_size
);
534 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
535 "Signature verification failed: 0x%lx", ERR_get_error());
539 #elif GCRYPT_VERSION_NUMBER >= 0x010600
540 gcry_sexp_t public_key_sexp
= NULL
, data_sexp
= NULL
, signature_sexp
= NULL
;
544 assert(signature_size
== key_size
* 2);
546 ge
= gcry_sexp_build(&signature_sexp
,
548 "(sig-val (eddsa (r %b) (s %b)))",
552 signature
+ key_size
);
558 ge
= gcry_sexp_build(&data_sexp
,
560 "(data (flags eddsa) (hash-algo sha512) (value %b))",
568 ge
= gcry_sexp_build(&public_key_sexp
,
570 "(public-key (ecc (curve %s) (flags eddsa) (q %b)))",
579 ge
= gcry_pk_verify(signature_sexp
, data_sexp
, public_key_sexp
);
580 if (gpg_err_code(ge
) == GPG_ERR_BAD_SIGNATURE
)
583 k
= log_debug_errno(SYNTHETIC_ERRNO(EIO
),
584 "EdDSA signature check failed: %s", gpg_strerror(ge
));
589 gcry_sexp_release(public_key_sexp
);
591 gcry_sexp_release(signature_sexp
);
593 gcry_sexp_release(data_sexp
);
601 static int dnssec_eddsa_verify(
603 const void *data
, size_t data_size
,
604 DnsResourceRecord
*rrsig
,
605 DnsResourceRecord
*dnskey
) {
606 elliptic_curve_t curve
;
609 if (algorithm
== DNSSEC_ALGORITHM_ED25519
) {
610 curve
= OPENSSL_OR_GCRYPT(NID_ED25519
, "Ed25519");
615 if (dnskey
->dnskey
.key_size
!= key_size
)
618 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
621 return dnssec_eddsa_verify_raw(
623 rrsig
->rrsig
.signature
, rrsig
->rrsig
.signature_size
,
625 dnskey
->dnskey
.key
, key_size
);
628 static int md_add_uint8(hash_context_t ctx
, uint8_t v
) {
630 return EVP_DigestUpdate(ctx
, &v
, sizeof(v
));
632 gcry_md_write(ctx
, &v
, sizeof(v
));
637 static int md_add_uint16(hash_context_t ctx
, uint16_t v
) {
640 return EVP_DigestUpdate(ctx
, &v
, sizeof(v
));
642 gcry_md_write(ctx
, &v
, sizeof(v
));
647 static void fwrite_uint8(FILE *fp
, uint8_t v
) {
648 fwrite(&v
, sizeof(v
), 1, fp
);
651 static void fwrite_uint16(FILE *fp
, uint16_t v
) {
653 fwrite(&v
, sizeof(v
), 1, fp
);
656 static void fwrite_uint32(FILE *fp
, uint32_t v
) {
658 fwrite(&v
, sizeof(v
), 1, fp
);
661 static int dnssec_rrsig_prepare(DnsResourceRecord
*rrsig
) {
662 int n_key_labels
, n_signer_labels
;
666 /* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source
667 * and .n_skip_labels_signer fields so that we can use them later on. */
670 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
672 /* Check if this RRSIG RR is already prepared */
673 if (rrsig
->n_skip_labels_source
!= UINT8_MAX
)
676 if (rrsig
->rrsig
.inception
> rrsig
->rrsig
.expiration
)
679 name
= dns_resource_key_name(rrsig
->key
);
681 n_key_labels
= dns_name_count_labels(name
);
682 if (n_key_labels
< 0)
684 if (rrsig
->rrsig
.labels
> n_key_labels
)
687 n_signer_labels
= dns_name_count_labels(rrsig
->rrsig
.signer
);
688 if (n_signer_labels
< 0)
689 return n_signer_labels
;
690 if (n_signer_labels
> rrsig
->rrsig
.labels
)
693 r
= dns_name_skip(name
, n_key_labels
- n_signer_labels
, &name
);
699 /* Check if the signer is really a suffix of us */
700 r
= dns_name_equal(name
, rrsig
->rrsig
.signer
);
706 assert(n_key_labels
< UINT8_MAX
); /* UINT8_MAX/-1 means unsigned. */
707 rrsig
->n_skip_labels_source
= n_key_labels
- rrsig
->rrsig
.labels
;
708 rrsig
->n_skip_labels_signer
= n_key_labels
- n_signer_labels
;
713 static int dnssec_rrsig_expired(DnsResourceRecord
*rrsig
, usec_t realtime
) {
714 usec_t expiration
, inception
, skew
;
717 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
719 if (realtime
== USEC_INFINITY
)
720 realtime
= now(CLOCK_REALTIME
);
722 expiration
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
723 inception
= rrsig
->rrsig
.inception
* USEC_PER_SEC
;
725 /* Consider inverted validity intervals as expired */
726 if (inception
> expiration
)
729 /* Permit a certain amount of clock skew of 10% of the valid
730 * time range. This takes inspiration from unbound's
732 skew
= (expiration
- inception
) / 10;
736 if (inception
< skew
)
741 if (expiration
+ skew
< expiration
)
742 expiration
= USEC_INFINITY
;
746 return realtime
< inception
|| realtime
> expiration
;
749 static hash_md_t
algorithm_to_implementation_id(uint8_t algorithm
) {
751 /* Translates a DNSSEC signature algorithm into an openssl/gcrypt digest identifier.
753 * Note that we implement all algorithms listed as "Must implement" and "Recommended to Implement" in
754 * RFC6944. We don't implement any algorithms that are listed as "Optional" or "Must Not Implement".
755 * Specifically, we do not implement RSAMD5, DSASHA1, DH, DSA-NSEC3-SHA1, and GOST-ECC. */
759 case DNSSEC_ALGORITHM_RSASHA1
:
760 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
761 return OPENSSL_OR_GCRYPT(EVP_sha1(), GCRY_MD_SHA1
);
763 case DNSSEC_ALGORITHM_RSASHA256
:
764 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
765 return OPENSSL_OR_GCRYPT(EVP_sha256(), GCRY_MD_SHA256
);
767 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
768 return OPENSSL_OR_GCRYPT(EVP_sha384(), GCRY_MD_SHA384
);
770 case DNSSEC_ALGORITHM_RSASHA512
:
771 return OPENSSL_OR_GCRYPT(EVP_sha512(), GCRY_MD_SHA512
);
774 return OPENSSL_OR_GCRYPT(NULL
, -EOPNOTSUPP
);
778 static void dnssec_fix_rrset_ttl(
779 DnsResourceRecord
*list
[],
781 DnsResourceRecord
*rrsig
) {
787 for (unsigned k
= 0; k
< n
; k
++) {
788 DnsResourceRecord
*rr
= list
[k
];
790 /* Pick the TTL as the minimum of the RR's TTL, the
791 * RR's original TTL according to the RRSIG and the
792 * RRSIG's own TTL, see RFC 4035, Section 5.3.3 */
793 rr
->ttl
= MIN3(rr
->ttl
, rrsig
->rrsig
.original_ttl
, rrsig
->ttl
);
794 rr
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
796 /* Copy over information about the signer and wildcard source of synthesis */
797 rr
->n_skip_labels_source
= rrsig
->n_skip_labels_source
;
798 rr
->n_skip_labels_signer
= rrsig
->n_skip_labels_signer
;
801 rrsig
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
804 static int dnssec_rrset_serialize_sig(
805 DnsResourceRecord
*rrsig
,
807 DnsResourceRecord
**list
,
811 size_t *ret_sig_size
) {
813 _cleanup_free_
char *sig_data
= NULL
;
815 _cleanup_fclose_
FILE *f
= NULL
;
816 uint8_t wire_format_name
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
817 DnsResourceRecord
*rr
;
822 assert(list
|| list_len
== 0);
823 assert(ret_sig_data
);
824 assert(ret_sig_size
);
826 f
= open_memstream_unlocked(&sig_data
, &sig_size
);
830 fwrite_uint16(f
, rrsig
->rrsig
.type_covered
);
831 fwrite_uint8(f
, rrsig
->rrsig
.algorithm
);
832 fwrite_uint8(f
, rrsig
->rrsig
.labels
);
833 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
834 fwrite_uint32(f
, rrsig
->rrsig
.expiration
);
835 fwrite_uint32(f
, rrsig
->rrsig
.inception
);
836 fwrite_uint16(f
, rrsig
->rrsig
.key_tag
);
838 r
= dns_name_to_wire_format(rrsig
->rrsig
.signer
, wire_format_name
, sizeof(wire_format_name
), true);
841 fwrite(wire_format_name
, 1, r
, f
);
843 /* Convert the source of synthesis into wire format */
844 r
= dns_name_to_wire_format(source
, wire_format_name
, sizeof(wire_format_name
), true);
848 for (size_t k
= 0; k
< list_len
; k
++) {
853 /* Hash the source of synthesis. If this is a wildcard, then prefix it with the *. label */
855 fwrite((uint8_t[]) { 1, '*'}, sizeof(uint8_t), 2, f
);
856 fwrite(wire_format_name
, 1, r
, f
);
858 fwrite_uint16(f
, rr
->key
->type
);
859 fwrite_uint16(f
, rr
->key
->class);
860 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
862 l
= DNS_RESOURCE_RECORD_RDATA_SIZE(rr
);
865 fwrite_uint16(f
, (uint16_t) l
);
866 fwrite(DNS_RESOURCE_RECORD_RDATA(rr
), 1, l
, f
);
869 r
= fflush_and_check(f
);
870 f
= safe_fclose(f
); /* sig_data may be reallocated when f is closed. */
874 *ret_sig_data
= TAKE_PTR(sig_data
);
875 *ret_sig_size
= sig_size
;
879 static int dnssec_rrset_verify_sig(
880 DnsResourceRecord
*rrsig
,
881 DnsResourceRecord
*dnskey
,
882 const char *sig_data
,
888 assert(sig_size
> 0);
890 hash_md_t md_algorithm
;
893 uint8_t hash
[EVP_MAX_MD_SIZE
];
896 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
900 initialize_libgcrypt(false);
903 switch (rrsig
->rrsig
.algorithm
) {
904 case DNSSEC_ALGORITHM_ED25519
:
905 #if PREFER_OPENSSL || GCRYPT_VERSION_NUMBER >= 0x010600
906 return dnssec_eddsa_verify(
907 rrsig
->rrsig
.algorithm
,
912 case DNSSEC_ALGORITHM_ED448
:
915 /* OK, the RRs are now in canonical order. Let's calculate the digest */
916 md_algorithm
= algorithm_to_implementation_id(rrsig
->rrsig
.algorithm
);
921 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
925 if (EVP_DigestInit_ex(ctx
, md_algorithm
, NULL
) <= 0)
928 if (EVP_DigestUpdate(ctx
, sig_data
, sig_size
) <= 0)
931 if (EVP_DigestFinal_ex(ctx
, hash
, &hash_size
) <= 0)
934 assert(hash_size
> 0);
937 if (md_algorithm
< 0)
940 gcry_error_t err
= gcry_md_open(&md
, md_algorithm
, 0);
941 if (gcry_err_code(err
) != GPG_ERR_NO_ERROR
|| !md
)
944 hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
945 assert(hash_size
> 0);
947 gcry_md_write(md
, sig_data
, sig_size
);
949 hash
= gcry_md_read(md
, 0);
955 switch (rrsig
->rrsig
.algorithm
) {
957 case DNSSEC_ALGORITHM_RSASHA1
:
958 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
959 case DNSSEC_ALGORITHM_RSASHA256
:
960 case DNSSEC_ALGORITHM_RSASHA512
:
961 return dnssec_rsa_verify(
962 OPENSSL_OR_GCRYPT(md_algorithm
, gcry_md_algo_name(md_algorithm
)),
967 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
968 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
969 return dnssec_ecdsa_verify(
970 OPENSSL_OR_GCRYPT(md_algorithm
, gcry_md_algo_name(md_algorithm
)),
971 rrsig
->rrsig
.algorithm
,
977 assert_not_reached();
981 int dnssec_verify_rrset(
983 const DnsResourceKey
*key
,
984 DnsResourceRecord
*rrsig
,
985 DnsResourceRecord
*dnskey
,
987 DnssecResult
*result
) {
989 DnsResourceRecord
**list
, *rr
;
990 const char *source
, *name
;
991 _cleanup_free_
char *sig_data
= NULL
;
992 size_t sig_size
= 0; /* avoid false maybe-uninitialized warning */
1001 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
1002 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
1004 /* Verifies that the RRSet matches the specified "key" in "a",
1005 * using the signature "rrsig" and the key "dnskey". It's
1006 * assumed that RRSIG and DNSKEY match. */
1008 r
= dnssec_rrsig_prepare(rrsig
);
1010 *result
= DNSSEC_INVALID
;
1016 r
= dnssec_rrsig_expired(rrsig
, realtime
);
1020 *result
= DNSSEC_SIGNATURE_EXPIRED
;
1024 name
= dns_resource_key_name(key
);
1026 /* Some keys may only appear signed in the zone apex, and are invalid anywhere else. (SOA, NS...) */
1027 if (dns_type_apex_only(rrsig
->rrsig
.type_covered
)) {
1028 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
1032 *result
= DNSSEC_INVALID
;
1037 /* OTOH DS RRs may not appear in the zone apex, but are valid everywhere else. */
1038 if (rrsig
->rrsig
.type_covered
== DNS_TYPE_DS
) {
1039 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
1043 *result
= DNSSEC_INVALID
;
1048 /* Determine the "Source of Synthesis" and whether this is a wildcard RRSIG */
1049 r
= dns_name_suffix(name
, rrsig
->rrsig
.labels
, &source
);
1052 if (r
> 0 && !dns_type_may_wildcard(rrsig
->rrsig
.type_covered
)) {
1053 /* We refuse to validate NSEC3 or SOA RRs that are synthesized from wildcards */
1054 *result
= DNSSEC_INVALID
;
1058 /* If we stripped a single label, then let's see if that maybe was "*". If so, we are not really
1059 * synthesized from a wildcard, we are the wildcard itself. Treat that like a normal name. */
1060 r
= dns_name_startswith(name
, "*");
1070 /* Collect all relevant RRs in a single array, so that we can look at the RRset */
1071 list
= newa(DnsResourceRecord
*, dns_answer_size(a
));
1073 DNS_ANSWER_FOREACH(rr
, a
) {
1074 r
= dns_resource_key_equal(key
, rr
->key
);
1080 /* We need the wire format for ordering, and digest calculation */
1081 r
= dns_resource_record_to_wire_format(rr
, true);
1087 if (n
> VERIFY_RRS_MAX
)
1094 /* Bring the RRs into canonical order */
1095 typesafe_qsort(list
, n
, rr_compare
);
1097 r
= dnssec_rrset_serialize_sig(rrsig
, source
, list
, n
, wildcard
,
1098 &sig_data
, &sig_size
);
1102 r
= dnssec_rrset_verify_sig(rrsig
, dnskey
, sig_data
, sig_size
);
1103 if (r
== -EOPNOTSUPP
) {
1104 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
1110 /* Now, fix the ttl, expiry, and remember the synthesizing source and the signer */
1112 dnssec_fix_rrset_ttl(list
, n
, rrsig
);
1115 *result
= DNSSEC_INVALID
;
1117 *result
= DNSSEC_VALIDATED_WILDCARD
;
1119 *result
= DNSSEC_VALIDATED
;
1124 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
1129 /* Checks if the specified DNSKEY RR matches the key used for
1130 * the signature in the specified RRSIG RR */
1132 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
1135 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1137 if (dnskey
->key
->class != rrsig
->key
->class)
1139 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
1141 if (!revoked_ok
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
1143 if (dnskey
->dnskey
.protocol
!= 3)
1145 if (dnskey
->dnskey
.algorithm
!= rrsig
->rrsig
.algorithm
)
1148 if (dnssec_keytag(dnskey
, false) != rrsig
->rrsig
.key_tag
)
1151 return dns_name_equal(dns_resource_key_name(dnskey
->key
), rrsig
->rrsig
.signer
);
1154 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
1158 /* Checks if the specified RRSIG RR protects the RRSet of the specified RR key. */
1160 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
1162 if (rrsig
->key
->class != key
->class)
1164 if (rrsig
->rrsig
.type_covered
!= key
->type
)
1167 return dns_name_equal(dns_resource_key_name(rrsig
->key
), dns_resource_key_name(key
));
1170 int dnssec_verify_rrset_search(
1172 const DnsResourceKey
*key
,
1173 DnsAnswer
*validated_dnskeys
,
1175 DnssecResult
*result
,
1176 DnsResourceRecord
**ret_rrsig
) {
1178 bool found_rrsig
= false, found_invalid
= false, found_expired_rrsig
= false, found_unsupported_algorithm
= false;
1179 DnsResourceRecord
*rrsig
;
1185 /* Verifies all RRs from "a" that match the key "key" against DNSKEYs in "validated_dnskeys" */
1187 if (dns_answer_isempty(a
))
1190 /* Iterate through each RRSIG RR. */
1191 DNS_ANSWER_FOREACH(rrsig
, a
) {
1192 DnsResourceRecord
*dnskey
;
1193 DnsAnswerFlags flags
;
1195 /* Is this an RRSIG RR that applies to RRs matching our key? */
1196 r
= dnssec_key_match_rrsig(key
, rrsig
);
1204 /* Look for a matching key */
1205 DNS_ANSWER_FOREACH_FLAGS(dnskey
, flags
, validated_dnskeys
) {
1206 DnssecResult one_result
;
1208 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1211 /* Is this a DNSKEY RR that matches they key of our RRSIG? */
1212 r
= dnssec_rrsig_match_dnskey(rrsig
, dnskey
, false);
1218 /* Take the time here, if it isn't set yet, so
1219 * that we do all validations with the same
1221 if (realtime
== USEC_INFINITY
)
1222 realtime
= now(CLOCK_REALTIME
);
1224 /* Yay, we found a matching RRSIG with a matching
1225 * DNSKEY, awesome. Now let's verify all entries of
1226 * the RRSet against the RRSIG and DNSKEY
1229 r
= dnssec_verify_rrset(a
, key
, rrsig
, dnskey
, realtime
, &one_result
);
1233 switch (one_result
) {
1235 case DNSSEC_VALIDATED
:
1236 case DNSSEC_VALIDATED_WILDCARD
:
1237 /* Yay, the RR has been validated,
1238 * return immediately, but fix up the expiry */
1242 *result
= one_result
;
1245 case DNSSEC_INVALID
:
1246 /* If the signature is invalid, let's try another
1247 key and/or signature. After all they
1248 key_tags and stuff are not unique, and
1249 might be shared by multiple keys. */
1250 found_invalid
= true;
1253 case DNSSEC_UNSUPPORTED_ALGORITHM
:
1254 /* If the key algorithm is
1255 unsupported, try another
1256 RRSIG/DNSKEY pair, but remember we
1257 encountered this, so that we can
1258 return a proper error when we
1259 encounter nothing better. */
1260 found_unsupported_algorithm
= true;
1263 case DNSSEC_SIGNATURE_EXPIRED
:
1264 /* If the signature is expired, try
1265 another one, but remember it, so
1266 that we can return this */
1267 found_expired_rrsig
= true;
1271 assert_not_reached();
1276 if (found_expired_rrsig
)
1277 *result
= DNSSEC_SIGNATURE_EXPIRED
;
1278 else if (found_unsupported_algorithm
)
1279 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
1280 else if (found_invalid
)
1281 *result
= DNSSEC_INVALID
;
1282 else if (found_rrsig
)
1283 *result
= DNSSEC_MISSING_KEY
;
1285 *result
= DNSSEC_NO_SIGNATURE
;
1293 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
1294 DnsResourceRecord
*rr
;
1297 /* Checks whether there's at least one RRSIG in 'a' that protects RRs of the specified key */
1299 DNS_ANSWER_FOREACH(rr
, a
) {
1300 r
= dnssec_key_match_rrsig(key
, rr
);
1310 static hash_md_t
digest_to_hash_md(uint8_t algorithm
) {
1312 /* Translates a DNSSEC digest algorithm into an openssl/gcrypt digest identifier */
1314 switch (algorithm
) {
1316 case DNSSEC_DIGEST_SHA1
:
1317 return OPENSSL_OR_GCRYPT(EVP_sha1(), GCRY_MD_SHA1
);
1319 case DNSSEC_DIGEST_SHA256
:
1320 return OPENSSL_OR_GCRYPT(EVP_sha256(), GCRY_MD_SHA256
);
1322 case DNSSEC_DIGEST_SHA384
:
1323 return OPENSSL_OR_GCRYPT(EVP_sha384(), GCRY_MD_SHA384
);
1326 return OPENSSL_OR_GCRYPT(NULL
, -EOPNOTSUPP
);
1330 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
1331 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1337 /* Implements DNSKEY verification by a DS, according to RFC 4035, section 5.2 */
1339 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1341 if (ds
->key
->type
!= DNS_TYPE_DS
)
1343 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
1344 return -EKEYREJECTED
;
1345 if (!mask_revoke
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
1346 return -EKEYREJECTED
;
1347 if (dnskey
->dnskey
.protocol
!= 3)
1348 return -EKEYREJECTED
;
1350 if (dnskey
->dnskey
.algorithm
!= ds
->ds
.algorithm
)
1352 if (dnssec_keytag(dnskey
, mask_revoke
) != ds
->ds
.key_tag
)
1355 r
= dns_name_to_wire_format(dns_resource_key_name(dnskey
->key
), wire_format
, sizeof wire_format
, true);
1359 hash_md_t md_algorithm
= digest_to_hash_md(ds
->ds
.digest_type
);
1365 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= NULL
;
1366 uint8_t result
[EVP_MAX_MD_SIZE
];
1368 unsigned hash_size
= EVP_MD_size(md_algorithm
);
1369 assert(hash_size
> 0);
1371 if (ds
->ds
.digest_size
!= hash_size
)
1374 ctx
= EVP_MD_CTX_new();
1378 if (EVP_DigestInit_ex(ctx
, md_algorithm
, NULL
) <= 0)
1381 if (EVP_DigestUpdate(ctx
, wire_format
, r
) <= 0)
1385 md_add_uint16(ctx
, dnskey
->dnskey
.flags
& ~DNSKEY_FLAG_REVOKE
);
1387 md_add_uint16(ctx
, dnskey
->dnskey
.flags
);
1389 r
= md_add_uint8(ctx
, dnskey
->dnskey
.protocol
);
1392 r
= md_add_uint8(ctx
, dnskey
->dnskey
.algorithm
);
1395 if (EVP_DigestUpdate(ctx
, dnskey
->dnskey
.key
, dnskey
->dnskey
.key_size
) <= 0)
1398 if (EVP_DigestFinal_ex(ctx
, result
, NULL
) <= 0)
1402 if (md_algorithm
< 0)
1405 initialize_libgcrypt(false);
1407 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
1409 size_t hash_size
= gcry_md_get_algo_dlen(md_algorithm
);
1410 assert(hash_size
> 0);
1412 if (ds
->ds
.digest_size
!= hash_size
)
1415 gcry_error_t err
= gcry_md_open(&md
, md_algorithm
, 0);
1416 if (gcry_err_code(err
) != GPG_ERR_NO_ERROR
|| !md
)
1419 gcry_md_write(md
, wire_format
, r
);
1421 md_add_uint16(md
, dnskey
->dnskey
.flags
& ~DNSKEY_FLAG_REVOKE
);
1423 md_add_uint16(md
, dnskey
->dnskey
.flags
);
1424 md_add_uint8(md
, dnskey
->dnskey
.protocol
);
1425 md_add_uint8(md
, dnskey
->dnskey
.algorithm
);
1426 gcry_md_write(md
, dnskey
->dnskey
.key
, dnskey
->dnskey
.key_size
);
1428 void *result
= gcry_md_read(md
, 0);
1433 return memcmp(result
, ds
->ds
.digest
, ds
->ds
.digest_size
) == 0;
1436 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
1437 DnsResourceRecord
*ds
;
1438 DnsAnswerFlags flags
;
1443 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1446 DNS_ANSWER_FOREACH_FLAGS(ds
, flags
, validated_ds
) {
1448 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1451 if (ds
->key
->type
!= DNS_TYPE_DS
)
1453 if (ds
->key
->class != dnskey
->key
->class)
1456 r
= dns_name_equal(dns_resource_key_name(dnskey
->key
), dns_resource_key_name(ds
->key
));
1462 r
= dnssec_verify_dnskey_by_ds(dnskey
, ds
, false);
1463 if (IN_SET(r
, -EKEYREJECTED
, -EOPNOTSUPP
))
1464 return 0; /* The DNSKEY is revoked or otherwise invalid, or we don't support the digest algorithm */
1474 static hash_md_t
nsec3_hash_to_hash_md(uint8_t algorithm
) {
1476 /* Translates a DNSSEC NSEC3 hash algorithm into an openssl/gcrypt digest identifier */
1478 switch (algorithm
) {
1480 case NSEC3_ALGORITHM_SHA1
:
1481 return OPENSSL_OR_GCRYPT(EVP_sha1(), GCRY_MD_SHA1
);
1484 return OPENSSL_OR_GCRYPT(NULL
, -EOPNOTSUPP
);
1488 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
1489 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1496 if (nsec3
->key
->type
!= DNS_TYPE_NSEC3
)
1499 if (nsec3
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1500 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1501 "Ignoring NSEC3 RR %s with excessive number of iterations.",
1502 dns_resource_record_to_string(nsec3
));
1504 hash_md_t algorithm
= nsec3_hash_to_hash_md(nsec3
->nsec3
.algorithm
);
1509 size_t hash_size
= EVP_MD_size(algorithm
);
1510 assert(hash_size
> 0);
1512 if (nsec3
->nsec3
.next_hashed_name_size
!= hash_size
)
1515 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
1519 if (EVP_DigestInit_ex(ctx
, algorithm
, NULL
) <= 0)
1522 r
= dns_name_to_wire_format(name
, wire_format
, sizeof(wire_format
), true);
1526 if (EVP_DigestUpdate(ctx
, wire_format
, r
) <= 0)
1528 if (EVP_DigestUpdate(ctx
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
) <= 0)
1531 uint8_t result
[EVP_MAX_MD_SIZE
];
1532 if (EVP_DigestFinal_ex(ctx
, result
, NULL
) <= 0)
1535 for (unsigned k
= 0; k
< nsec3
->nsec3
.iterations
; k
++) {
1536 if (EVP_DigestInit_ex(ctx
, algorithm
, NULL
) <= 0)
1538 if (EVP_DigestUpdate(ctx
, result
, hash_size
) <= 0)
1540 if (EVP_DigestUpdate(ctx
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
) <= 0)
1543 if (EVP_DigestFinal_ex(ctx
, result
, NULL
) <= 0)
1550 initialize_libgcrypt(false);
1552 unsigned hash_size
= gcry_md_get_algo_dlen(algorithm
);
1553 assert(hash_size
> 0);
1555 if (nsec3
->nsec3
.next_hashed_name_size
!= hash_size
)
1558 r
= dns_name_to_wire_format(name
, wire_format
, sizeof(wire_format
), true);
1562 _cleanup_(gcry_md_closep
) gcry_md_hd_t md
= NULL
;
1563 gcry_error_t err
= gcry_md_open(&md
, algorithm
, 0);
1564 if (gcry_err_code(err
) != GPG_ERR_NO_ERROR
|| !md
)
1567 gcry_md_write(md
, wire_format
, r
);
1568 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1570 void *result
= gcry_md_read(md
, 0);
1574 for (unsigned k
= 0; k
< nsec3
->nsec3
.iterations
; k
++) {
1575 uint8_t tmp
[hash_size
];
1576 memcpy(tmp
, result
, hash_size
);
1579 gcry_md_write(md
, tmp
, hash_size
);
1580 gcry_md_write(md
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
);
1582 result
= gcry_md_read(md
, 0);
1588 memcpy(ret
, result
, hash_size
);
1589 return (int) hash_size
;
1592 static int nsec3_is_good(DnsResourceRecord
*rr
, DnsResourceRecord
*nsec3
) {
1598 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
1601 /* RFC 5155, Section 8.2 says we MUST ignore NSEC3 RRs with flags != 0 or 1 */
1602 if (!IN_SET(rr
->nsec3
.flags
, 0, 1))
1605 /* Ignore NSEC3 RRs whose algorithm we don't know */
1607 if (!nsec3_hash_to_hash_md(rr
->nsec3
.algorithm
))
1610 if (nsec3_hash_to_hash_md(rr
->nsec3
.algorithm
) < 0)
1614 /* Ignore NSEC3 RRs with an excessive number of required iterations */
1615 if (rr
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1618 /* Ignore NSEC3 RRs generated from wildcards. If these NSEC3 RRs weren't correctly signed we can't make this
1619 * check (since rr->n_skip_labels_source is -1), but that's OK, as we won't trust them anyway in that case. */
1620 if (!IN_SET(rr
->n_skip_labels_source
, 0, UINT8_MAX
))
1622 /* Ignore NSEC3 RRs that are located anywhere else than one label below the zone */
1623 if (!IN_SET(rr
->n_skip_labels_signer
, 1, UINT8_MAX
))
1629 /* If a second NSEC3 RR is specified, also check if they are from the same zone. */
1631 if (nsec3
== rr
) /* Shortcut */
1634 if (rr
->key
->class != nsec3
->key
->class)
1636 if (rr
->nsec3
.algorithm
!= nsec3
->nsec3
.algorithm
)
1638 if (rr
->nsec3
.iterations
!= nsec3
->nsec3
.iterations
)
1640 if (rr
->nsec3
.salt_size
!= nsec3
->nsec3
.salt_size
)
1642 if (memcmp_safe(rr
->nsec3
.salt
, nsec3
->nsec3
.salt
, rr
->nsec3
.salt_size
) != 0)
1645 a
= dns_resource_key_name(rr
->key
);
1646 r
= dns_name_parent(&a
); /* strip off hash */
1650 b
= dns_resource_key_name(nsec3
->key
);
1651 r
= dns_name_parent(&b
); /* strip off hash */
1655 /* Make sure both have the same parent */
1656 return dns_name_equal(a
, b
);
1659 static int nsec3_hashed_domain_format(const uint8_t *hashed
, size_t hashed_size
, const char *zone
, char **ret
) {
1660 _cleanup_free_
char *l
= NULL
;
1664 assert(hashed_size
> 0);
1668 l
= base32hexmem(hashed
, hashed_size
, false);
1672 j
= strjoin(l
, ".", zone
);
1677 return (int) hashed_size
;
1680 static int nsec3_hashed_domain_make(DnsResourceRecord
*nsec3
, const char *domain
, const char *zone
, char **ret
) {
1681 uint8_t hashed
[DNSSEC_HASH_SIZE_MAX
];
1689 hashed_size
= dnssec_nsec3_hash(nsec3
, domain
, hashed
);
1690 if (hashed_size
< 0)
1693 return nsec3_hashed_domain_format(hashed
, (size_t) hashed_size
, zone
, ret
);
1696 /* See RFC 5155, Section 8
1697 * First try to find a NSEC3 record that matches our query precisely, if that fails, find the closest
1698 * enclosure. Secondly, find a proof that there is no closer enclosure and either a proof that there
1699 * is no wildcard domain as a direct descendant of the closest enclosure, or find an NSEC3 record that
1700 * matches the wildcard domain.
1702 * Based on this we can prove either the existence of the record in @key, or NXDOMAIN or NODATA, or
1703 * that there is no proof either way. The latter is the case if a proof of non-existence of a given
1704 * name uses an NSEC3 record with the opt-out bit set. Lastly, if we are given insufficient NSEC3 records
1705 * to conclude anything we indicate this by returning NO_RR. */
1706 static int dnssec_test_nsec3(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1707 _cleanup_free_
char *next_closer_domain
= NULL
, *wildcard_domain
= NULL
;
1708 const char *zone
, *p
, *pp
= NULL
, *wildcard
;
1709 DnsResourceRecord
*rr
, *enclosure_rr
, *zone_rr
, *wildcard_rr
= NULL
;
1710 DnsAnswerFlags flags
;
1712 bool a
, no_closer
= false, no_wildcard
= false, optout
= false;
1717 /* First step, find the zone name and the NSEC3 parameters of the zone.
1718 * it is sufficient to look for the longest common suffix we find with
1719 * any NSEC3 RR in the response. Any NSEC3 record will do as all NSEC3
1720 * records from a given zone in a response must use the same
1722 zone
= dns_resource_key_name(key
);
1724 DNS_ANSWER_FOREACH_FLAGS(zone_rr
, flags
, answer
) {
1725 r
= nsec3_is_good(zone_rr
, NULL
);
1731 r
= dns_name_equal_skip(dns_resource_key_name(zone_rr
->key
), 1, zone
);
1738 /* Strip one label from the front */
1739 r
= dns_name_parent(&zone
);
1746 *result
= DNSSEC_NSEC_NO_RR
;
1750 /* Second step, find the closest encloser NSEC3 RR in 'answer' that matches 'key' */
1751 p
= dns_resource_key_name(key
);
1753 _cleanup_free_
char *hashed_domain
= NULL
;
1755 hashed_size
= nsec3_hashed_domain_make(zone_rr
, p
, zone
, &hashed_domain
);
1756 if (hashed_size
== -EOPNOTSUPP
) {
1757 *result
= DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
;
1760 if (hashed_size
< 0)
1763 DNS_ANSWER_FOREACH_FLAGS(enclosure_rr
, flags
, answer
) {
1765 r
= nsec3_is_good(enclosure_rr
, zone_rr
);
1771 if (enclosure_rr
->nsec3
.next_hashed_name_size
!= (size_t) hashed_size
)
1774 r
= dns_name_equal(dns_resource_key_name(enclosure_rr
->key
), hashed_domain
);
1778 a
= flags
& DNS_ANSWER_AUTHENTICATED
;
1779 goto found_closest_encloser
;
1783 /* We didn't find the closest encloser with this name,
1784 * but let's remember this domain name, it might be
1785 * the next closer name */
1789 /* Strip one label from the front */
1790 r
= dns_name_parent(&p
);
1797 *result
= DNSSEC_NSEC_NO_RR
;
1800 found_closest_encloser
:
1801 /* We found a closest encloser in 'p'; next closer is 'pp' */
1804 /* We have an exact match! If we area looking for a DS RR, then we must insist that we got the NSEC3 RR
1805 * from the parent. Otherwise the one from the child. Do so, by checking whether SOA and NS are
1806 * appropriately set. */
1808 if (key
->type
== DNS_TYPE_DS
) {
1809 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1812 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1813 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1817 /* No next closer NSEC3 RR. That means there's a direct NSEC3 RR for our key. */
1818 if (bitmap_isset(enclosure_rr
->nsec3
.types
, key
->type
))
1819 *result
= DNSSEC_NSEC_FOUND
;
1820 else if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1821 *result
= DNSSEC_NSEC_CNAME
;
1823 *result
= DNSSEC_NSEC_NODATA
;
1828 *ttl
= enclosure_rr
->ttl
;
1833 /* Ensure this is not a DNAME domain, see RFC5155, section 8.3. */
1834 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_DNAME
))
1837 /* Ensure that this data is from the delegated domain
1838 * (i.e. originates from the "lower" DNS server), and isn't
1839 * just glue records (i.e. doesn't originate from the "upper"
1841 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1842 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1845 /* Prove that there is no next closer and whether or not there is a wildcard domain. */
1847 wildcard
= strjoina("*.", p
);
1848 r
= nsec3_hashed_domain_make(enclosure_rr
, wildcard
, zone
, &wildcard_domain
);
1851 if (r
!= hashed_size
)
1854 r
= nsec3_hashed_domain_make(enclosure_rr
, pp
, zone
, &next_closer_domain
);
1857 if (r
!= hashed_size
)
1860 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1861 _cleanup_free_
char *next_hashed_domain
= NULL
;
1863 r
= nsec3_is_good(rr
, zone_rr
);
1869 r
= nsec3_hashed_domain_format(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, zone
, &next_hashed_domain
);
1873 r
= dns_name_between(dns_resource_key_name(rr
->key
), next_closer_domain
, next_hashed_domain
);
1877 if (rr
->nsec3
.flags
& 1)
1880 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1885 r
= dns_name_equal(dns_resource_key_name(rr
->key
), wildcard_domain
);
1889 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1894 r
= dns_name_between(dns_resource_key_name(rr
->key
), wildcard_domain
, next_hashed_domain
);
1898 if (rr
->nsec3
.flags
& 1)
1899 /* This only makes sense if we have a wildcard delegation, which is
1900 * very unlikely, see RFC 4592, Section 4.2, but we cannot rely on
1901 * this not happening, so hence cannot simply conclude NXDOMAIN as
1905 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1911 if (wildcard_rr
&& no_wildcard
)
1915 *result
= DNSSEC_NSEC_NO_RR
;
1920 /* A wildcard exists that matches our query. */
1922 /* This is not specified in any RFC to the best of my knowledge, but
1923 * if the next closer enclosure is covered by an opt-out NSEC3 RR
1924 * it means that we cannot prove that the source of synthesis is
1925 * correct, as there may be a closer match. */
1926 *result
= DNSSEC_NSEC_OPTOUT
;
1927 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, key
->type
))
1928 *result
= DNSSEC_NSEC_FOUND
;
1929 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1930 *result
= DNSSEC_NSEC_CNAME
;
1932 *result
= DNSSEC_NSEC_NODATA
;
1935 /* The RFC only specifies that we have to care for optout for NODATA for
1936 * DS records. However, children of an insecure opt-out delegation should
1937 * also be considered opt-out, rather than verified NXDOMAIN.
1938 * Note that we do not require a proof of wildcard non-existence if the
1939 * next closer domain is covered by an opt-out, as that would not provide
1940 * any additional information. */
1941 *result
= DNSSEC_NSEC_OPTOUT
;
1942 else if (no_wildcard
)
1943 *result
= DNSSEC_NSEC_NXDOMAIN
;
1945 *result
= DNSSEC_NSEC_NO_RR
;
1955 *ttl
= enclosure_rr
->ttl
;
1960 static int dnssec_nsec_wildcard_equal(DnsResourceRecord
*rr
, const char *name
) {
1961 char label
[DNS_LABEL_MAX
];
1966 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1968 /* Checks whether the specified RR has a name beginning in "*.", and if the rest is a suffix of our name */
1970 if (rr
->n_skip_labels_source
!= 1)
1973 n
= dns_resource_key_name(rr
->key
);
1974 r
= dns_label_unescape(&n
, label
, sizeof label
, 0);
1977 if (r
!= 1 || label
[0] != '*')
1980 return dns_name_endswith(name
, n
);
1983 static int dnssec_nsec_in_path(DnsResourceRecord
*rr
, const char *name
) {
1984 const char *nn
, *common_suffix
;
1988 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1990 /* Checks whether the specified nsec RR indicates that name is an empty non-terminal (ENT)
1992 * A couple of examples:
1994 * NSEC bar → waldo.foo.bar: indicates that foo.bar exists and is an ENT
1995 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that xoo.bar and zzz.xoo.bar exist and are ENTs
1996 * NSEC yyy.zzz.xoo.bar → bar: indicates pretty much nothing about ENTs
1999 /* First, determine parent of next domain. */
2000 nn
= rr
->nsec
.next_domain_name
;
2001 r
= dns_name_parent(&nn
);
2005 /* If the name we just determined is not equal or child of the name we are interested in, then we can't say
2006 * anything at all. */
2007 r
= dns_name_endswith(nn
, name
);
2011 /* 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. */
2012 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
2016 return dns_name_endswith(name
, common_suffix
);
2019 static int dnssec_nsec_from_parent_zone(DnsResourceRecord
*rr
, const char *name
) {
2023 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
2025 /* Checks whether this NSEC originates to the parent zone or the child zone. */
2027 r
= dns_name_parent(&name
);
2031 r
= dns_name_equal(name
, dns_resource_key_name(rr
->key
));
2035 /* DNAME, and NS without SOA is an indication for a delegation. */
2036 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_DNAME
))
2039 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) && !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
2045 static int dnssec_nsec_covers(DnsResourceRecord
*rr
, const char *name
) {
2050 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
2052 /* Checks whether the name is covered by this NSEC RR. This means, that the name is somewhere below the NSEC's
2053 * signer name, and between the NSEC's two names. */
2055 r
= dns_resource_record_signer(rr
, &signer
);
2059 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
2063 return dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
2066 static int dnssec_nsec_generate_wildcard(DnsResourceRecord
*rr
, const char *name
, char **wc
) {
2067 const char *common_suffix1
, *common_suffix2
, *signer
;
2068 int r
, labels1
, labels2
;
2071 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
2073 /* Generates "Wildcard at the Closest Encloser" for the given name and NSEC RR. */
2075 r
= dns_resource_record_signer(rr
, &signer
);
2079 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
2083 r
= dns_name_common_suffix(name
, dns_resource_key_name(rr
->key
), &common_suffix1
);
2087 r
= dns_name_common_suffix(name
, rr
->nsec
.next_domain_name
, &common_suffix2
);
2091 labels1
= dns_name_count_labels(common_suffix1
);
2095 labels2
= dns_name_count_labels(common_suffix2
);
2099 if (labels1
> labels2
)
2100 r
= dns_name_concat("*", common_suffix1
, 0, wc
);
2102 r
= dns_name_concat("*", common_suffix2
, 0, wc
);
2110 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
2111 bool have_nsec3
= false, covering_rr_authenticated
= false, wildcard_rr_authenticated
= false;
2112 DnsResourceRecord
*rr
, *covering_rr
= NULL
, *wildcard_rr
= NULL
;
2113 DnsAnswerFlags flags
;
2120 /* Look for any NSEC/NSEC3 RRs that say something about the specified key. */
2122 name
= dns_resource_key_name(key
);
2124 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
2126 if (rr
->key
->class != key
->class)
2129 have_nsec3
= have_nsec3
|| (rr
->key
->type
== DNS_TYPE_NSEC3
);
2131 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
2134 /* The following checks only make sense for NSEC RRs that are not expanded from a wildcard */
2135 r
= dns_resource_record_is_synthetic(rr
);
2136 if (r
== -ENODATA
) /* No signing RR known. */
2143 /* Check if this is a direct match. If so, we have encountered a NODATA case */
2144 r
= dns_name_equal(dns_resource_key_name(rr
->key
), name
);
2148 /* If it's not a direct match, maybe it's a wild card match? */
2149 r
= dnssec_nsec_wildcard_equal(rr
, name
);
2154 if (key
->type
== DNS_TYPE_DS
) {
2155 /* If we look for a DS RR and the server sent us the NSEC RR of the child zone
2156 * we have a problem. For DS RRs we want the NSEC RR from the parent */
2157 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
2160 /* For all RR types, ensure that if NS is set SOA is set too, so that we know
2161 * we got the child's NSEC. */
2162 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) &&
2163 !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
2167 if (bitmap_isset(rr
->nsec
.types
, key
->type
))
2168 *result
= DNSSEC_NSEC_FOUND
;
2169 else if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_CNAME
))
2170 *result
= DNSSEC_NSEC_CNAME
;
2172 *result
= DNSSEC_NSEC_NODATA
;
2175 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2182 /* Check if the name we are looking for is an empty non-terminal within the owner or next name
2183 * of the NSEC RR. */
2184 r
= dnssec_nsec_in_path(rr
, name
);
2188 *result
= DNSSEC_NSEC_NODATA
;
2191 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2198 /* The following two "covering" checks, are not useful if the NSEC is from the parent */
2199 r
= dnssec_nsec_from_parent_zone(rr
, name
);
2205 /* Check if this NSEC RR proves the absence of an explicit RR under this name */
2206 r
= dnssec_nsec_covers(rr
, name
);
2209 if (r
> 0 && (!covering_rr
|| !covering_rr_authenticated
)) {
2211 covering_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2216 _cleanup_free_
char *wc
= NULL
;
2217 r
= dnssec_nsec_generate_wildcard(covering_rr
, name
, &wc
);
2221 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
2223 if (rr
->key
->class != key
->class)
2226 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
2229 /* Check if this NSEC RR proves the nonexistence of the wildcard */
2230 r
= dnssec_nsec_covers(rr
, wc
);
2233 if (r
> 0 && (!wildcard_rr
|| !wildcard_rr_authenticated
)) {
2235 wildcard_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2240 if (covering_rr
&& wildcard_rr
) {
2241 /* If we could prove that neither the name itself, nor the wildcard at the closest encloser exists, we
2242 * proved the NXDOMAIN case. */
2243 *result
= DNSSEC_NSEC_NXDOMAIN
;
2246 *authenticated
= covering_rr_authenticated
&& wildcard_rr_authenticated
;
2248 *ttl
= MIN(covering_rr
->ttl
, wildcard_rr
->ttl
);
2253 /* OK, this was not sufficient. Let's see if NSEC3 can help. */
2255 return dnssec_test_nsec3(answer
, key
, result
, authenticated
, ttl
);
2257 /* No appropriate NSEC RR found, report this. */
2258 *result
= DNSSEC_NSEC_NO_RR
;
2262 static int dnssec_nsec_test_enclosed(DnsAnswer
*answer
, uint16_t type
, const char *name
, const char *zone
, bool *authenticated
) {
2263 DnsResourceRecord
*rr
;
2264 DnsAnswerFlags flags
;
2270 /* Checks whether there's an NSEC/NSEC3 that proves that the specified 'name' is non-existing in the specified
2271 * 'zone'. The 'zone' must be a suffix of the 'name'. */
2273 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
2276 if (rr
->key
->type
!= type
&& type
!= DNS_TYPE_ANY
)
2279 switch (rr
->key
->type
) {
2283 /* We only care for NSEC RRs from the indicated zone */
2284 r
= dns_resource_record_is_signer(rr
, zone
);
2290 r
= dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
2297 case DNS_TYPE_NSEC3
: {
2298 _cleanup_free_
char *hashed_domain
= NULL
, *next_hashed_domain
= NULL
;
2300 /* We only care for NSEC3 RRs from the indicated zone */
2301 r
= dns_resource_record_is_signer(rr
, zone
);
2307 r
= nsec3_is_good(rr
, NULL
);
2313 /* Format the domain we are testing with the NSEC3 RR's hash function */
2314 r
= nsec3_hashed_domain_make(
2321 if ((size_t) r
!= rr
->nsec3
.next_hashed_name_size
)
2324 /* Format the NSEC3's next hashed name as proper domain name */
2325 r
= nsec3_hashed_domain_format(
2326 rr
->nsec3
.next_hashed_name
,
2327 rr
->nsec3
.next_hashed_name_size
,
2329 &next_hashed_domain
);
2333 r
= dns_name_between(dns_resource_key_name(rr
->key
), hashed_domain
, next_hashed_domain
);
2347 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2355 static int dnssec_test_positive_wildcard_nsec3(
2360 bool *authenticated
) {
2362 const char *next_closer
= NULL
;
2365 /* Run a positive NSEC3 wildcard proof. Specifically:
2367 * A proof that the "next closer" of the generating wildcard does not exist.
2369 * Note a key difference between the NSEC3 and NSEC versions of the proof. NSEC RRs don't have to exist for
2370 * empty non-transients. NSEC3 RRs however have to. This means it's sufficient to check if the next closer name
2371 * exists for the NSEC3 RR and we are done.
2373 * 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
2374 * c.d.e.f does not exist. */
2378 r
= dns_name_parent(&name
);
2382 r
= dns_name_equal(name
, source
);
2389 return dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC3
, next_closer
, zone
, authenticated
);
2392 static int dnssec_test_positive_wildcard_nsec(
2397 bool *_authenticated
) {
2399 bool authenticated
= true;
2402 /* Run a positive NSEC wildcard proof. Specifically:
2404 * A proof that there's neither a wildcard name nor a non-wildcard name that is a suffix of the name "name" and
2405 * a prefix of the synthesizing source "source" in the zone "zone".
2407 * See RFC 5155, Section 8.8 and RFC 4035, Section 5.3.4
2409 * 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
2410 * have to prove that none of the following exist:
2420 _cleanup_free_
char *wc
= NULL
;
2423 /* Check if there's an NSEC or NSEC3 RR that proves that the mame we determined is really non-existing,
2424 * i.e between the owner name and the next name of an NSEC RR. */
2425 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, name
, zone
, &a
);
2429 authenticated
= authenticated
&& a
;
2431 /* Strip one label off */
2432 r
= dns_name_parent(&name
);
2436 /* Did we reach the source of synthesis? */
2437 r
= dns_name_equal(name
, source
);
2441 /* Successful exit */
2442 *_authenticated
= authenticated
;
2446 /* Safety check, that the source of synthesis is still our suffix */
2447 r
= dns_name_endswith(name
, source
);
2453 /* Replace the label we stripped off with an asterisk */
2454 wc
= strjoin("*.", name
);
2458 /* And check if the proof holds for the asterisk name, too */
2459 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, wc
, zone
, &a
);
2463 authenticated
= authenticated
&& a
;
2464 /* In the next iteration we'll check the non-asterisk-prefixed version */
2468 int dnssec_test_positive_wildcard(
2473 bool *authenticated
) {
2480 assert(authenticated
);
2482 r
= dns_answer_contains_zone_nsec3(answer
, zone
);
2486 return dnssec_test_positive_wildcard_nsec3(answer
, name
, source
, zone
, authenticated
);
2488 return dnssec_test_positive_wildcard_nsec(answer
, name
, source
, zone
, authenticated
);
2493 int dnssec_verify_rrset(
2495 const DnsResourceKey
*key
,
2496 DnsResourceRecord
*rrsig
,
2497 DnsResourceRecord
*dnskey
,
2499 DnssecResult
*result
) {
2504 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
2509 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
2514 int dnssec_verify_rrset_search(
2516 const DnsResourceKey
*key
,
2517 DnsAnswer
*validated_dnskeys
,
2519 DnssecResult
*result
,
2520 DnsResourceRecord
**ret_rrsig
) {
2525 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
2530 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
2535 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
2540 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
2545 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
2550 int dnssec_test_positive_wildcard(
2555 bool *authenticated
) {
2562 static const char* const dnssec_result_table
[_DNSSEC_RESULT_MAX
] = {
2563 [DNSSEC_VALIDATED
] = "validated",
2564 [DNSSEC_VALIDATED_WILDCARD
] = "validated-wildcard",
2565 [DNSSEC_INVALID
] = "invalid",
2566 [DNSSEC_SIGNATURE_EXPIRED
] = "signature-expired",
2567 [DNSSEC_UNSUPPORTED_ALGORITHM
] = "unsupported-algorithm",
2568 [DNSSEC_NO_SIGNATURE
] = "no-signature",
2569 [DNSSEC_MISSING_KEY
] = "missing-key",
2570 [DNSSEC_UNSIGNED
] = "unsigned",
2571 [DNSSEC_FAILED_AUXILIARY
] = "failed-auxiliary",
2572 [DNSSEC_NSEC_MISMATCH
] = "nsec-mismatch",
2573 [DNSSEC_INCOMPATIBLE_SERVER
] = "incompatible-server",
2575 DEFINE_STRING_TABLE_LOOKUP(dnssec_result
, DnssecResult
);
2577 static const char* const dnssec_verdict_table
[_DNSSEC_VERDICT_MAX
] = {
2578 [DNSSEC_SECURE
] = "secure",
2579 [DNSSEC_INSECURE
] = "insecure",
2580 [DNSSEC_BOGUS
] = "bogus",
2581 [DNSSEC_INDETERMINATE
] = "indeterminate",
2583 DEFINE_STRING_TABLE_LOOKUP(dnssec_verdict
, DnssecVerdict
);