1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "alloc-util.h"
5 #include "dns-domain.h"
9 #include "memory-util.h"
10 #include "memstream-util.h"
11 #include "openssl-util.h"
12 #include "resolved-dns-answer.h"
13 #include "resolved-dns-dnssec.h"
14 #include "resolved-dns-rr.h"
15 #include "sort-util.h"
16 #include "string-table.h"
17 #include "string-util.h"
18 #include "time-util.h"
20 #if HAVE_OPENSSL && OPENSSL_VERSION_MAJOR >= 3
21 DISABLE_WARNING_DEPRECATED_DECLARATIONS
;
22 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(RSA
*, RSA_free
, NULL
);
23 DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(EC_KEY
*, EC_KEY_free
, NULL
);
27 #define VERIFY_RRS_MAX 256
28 #define MAX_KEY_SIZE (32*1024)
30 /* Permit a maximum clock skew of 1h 10min. This should be enough to deal with DST confusion */
31 #define SKEW_MAX (1*USEC_PER_HOUR + 10*USEC_PER_MINUTE)
33 /* Maximum number of NSEC3 iterations we'll do. RFC5155 says 2500 shall be the maximum useful value, but
34 * RFC9276 § 3.2 says that we should reduce the acceptable iteration count */
35 #define NSEC3_ITERATIONS_MAX 100
38 * The DNSSEC Chain of trust:
40 * Normal RRs are protected via RRSIG RRs in combination with DNSKEY RRs, all in the same zone
41 * DNSKEY RRs are either protected like normal RRs, or via a DS from a zone "higher" up the tree
42 * DS RRs are protected like normal RRs
45 * Normal RR → RRSIG/DNSKEY+ → DS → RRSIG/DNSKEY+ → DS → ... → DS → RRSIG/DNSKEY+ → DS
48 uint16_t dnssec_keytag(DnsResourceRecord
*dnskey
, bool mask_revoke
) {
52 /* The algorithm from RFC 4034, Appendix B. */
55 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
57 f
= (uint32_t) dnskey
->dnskey
.flags
;
60 f
&= ~DNSKEY_FLAG_REVOKE
;
62 sum
= f
+ ((((uint32_t) dnskey
->dnskey
.protocol
) << 8) + (uint32_t) dnskey
->dnskey
.algorithm
);
64 p
= dnskey
->dnskey
.key
;
66 for (size_t i
= 0; i
< dnskey
->dnskey
.key_size
; i
++)
67 sum
+= (i
& 1) == 0 ? (uint32_t) p
[i
] << 8 : (uint32_t) p
[i
];
69 sum
+= (sum
>> 16) & UINT32_C(0xFFFF);
71 return sum
& UINT32_C(0xFFFF);
76 static int rr_compare(DnsResourceRecord
* const *a
, DnsResourceRecord
* const *b
) {
77 const DnsResourceRecord
*x
= *a
, *y
= *b
;
81 /* Let's order the RRs according to RFC 4034, Section 6.3 */
84 assert(x
->wire_format
);
86 assert(y
->wire_format
);
88 m
= MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(x
), DNS_RESOURCE_RECORD_RDATA_SIZE(y
));
90 r
= memcmp(DNS_RESOURCE_RECORD_RDATA(x
), DNS_RESOURCE_RECORD_RDATA(y
), m
);
94 return CMP(DNS_RESOURCE_RECORD_RDATA_SIZE(x
), DNS_RESOURCE_RECORD_RDATA_SIZE(y
));
97 static int dnssec_rsa_verify_raw(
98 const EVP_MD
*hash_algorithm
,
99 const void *signature
, size_t signature_size
,
100 const void *data
, size_t data_size
,
101 const void *exponent
, size_t exponent_size
,
102 const void *modulus
, size_t modulus_size
) {
105 DISABLE_WARNING_DEPRECATED_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());
160 static int dnssec_rsa_verify(
161 const EVP_MD
*hash_algorithm
,
162 const void *hash
, size_t hash_size
,
163 DnsResourceRecord
*rrsig
,
164 DnsResourceRecord
*dnskey
) {
166 size_t exponent_size
, modulus_size
;
167 void *exponent
, *modulus
;
169 assert(hash_algorithm
);
171 assert(hash_size
> 0);
175 if (*(uint8_t*) dnskey
->dnskey
.key
== 0) {
176 /* exponent is > 255 bytes long */
178 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 3;
180 ((size_t) (((uint8_t*) dnskey
->dnskey
.key
)[1]) << 8) |
181 ((size_t) ((uint8_t*) dnskey
->dnskey
.key
)[2]);
183 if (exponent_size
< 256)
186 if (3 + exponent_size
>= dnskey
->dnskey
.key_size
)
189 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 3 + exponent_size
;
190 modulus_size
= dnskey
->dnskey
.key_size
- 3 - exponent_size
;
193 /* exponent is <= 255 bytes long */
195 exponent
= (uint8_t*) dnskey
->dnskey
.key
+ 1;
196 exponent_size
= (size_t) ((uint8_t*) dnskey
->dnskey
.key
)[0];
198 if (exponent_size
<= 0)
201 if (1 + exponent_size
>= dnskey
->dnskey
.key_size
)
204 modulus
= (uint8_t*) dnskey
->dnskey
.key
+ 1 + exponent_size
;
205 modulus_size
= dnskey
->dnskey
.key_size
- 1 - exponent_size
;
208 return dnssec_rsa_verify_raw(
210 rrsig
->rrsig
.signature
, rrsig
->rrsig
.signature_size
,
212 exponent
, exponent_size
,
213 modulus
, modulus_size
);
216 static int dnssec_ecdsa_verify_raw(
217 const EVP_MD
*hash_algorithm
,
219 const void *signature_r
, size_t signature_r_size
,
220 const void *signature_s
, size_t signature_s_size
,
221 const void *data
, size_t data_size
,
222 const void *key
, size_t key_size
) {
225 DISABLE_WARNING_DEPRECATED_DECLARATIONS
;
226 _cleanup_(EC_GROUP_freep
) EC_GROUP
*ec_group
= NULL
;
227 _cleanup_(EC_POINT_freep
) EC_POINT
*p
= NULL
;
228 _cleanup_(EC_KEY_freep
) EC_KEY
*eckey
= NULL
;
229 _cleanup_(BN_CTX_freep
) BN_CTX
*bctx
= NULL
;
230 _cleanup_(BN_freep
) BIGNUM
*r
= NULL
, *s
= NULL
;
231 _cleanup_(ECDSA_SIG_freep
) ECDSA_SIG
*sig
= NULL
;
233 assert(hash_algorithm
);
235 ec_group
= EC_GROUP_new_by_curve_name(curve
);
239 p
= EC_POINT_new(ec_group
);
247 if (EC_POINT_oct2point(ec_group
, p
, key
, key_size
, bctx
) <= 0)
250 eckey
= EC_KEY_new();
254 if (EC_KEY_set_group(eckey
, ec_group
) <= 0)
257 if (EC_KEY_set_public_key(eckey
, p
) <= 0)
258 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
259 "EC_POINT_bn2point failed: 0x%lx", ERR_get_error());
261 assert(EC_KEY_check_key(eckey
) == 1);
263 r
= BN_bin2bn(signature_r
, signature_r_size
, NULL
);
267 s
= BN_bin2bn(signature_s
, signature_s_size
, NULL
);
271 /* TODO: We should eventually use the EVP API once it supports ECDSA signature verification */
273 sig
= ECDSA_SIG_new();
277 if (ECDSA_SIG_set0(sig
, r
, s
) <= 0)
281 k
= ECDSA_do_verify(data
, data_size
, sig
, eckey
);
283 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
284 "Signature verification failed: 0x%lx", ERR_get_error());
290 static int dnssec_ecdsa_verify(
291 const EVP_MD
*hash_algorithm
,
293 const void *hash
, size_t hash_size
,
294 DnsResourceRecord
*rrsig
,
295 DnsResourceRecord
*dnskey
) {
306 if (algorithm
== DNSSEC_ALGORITHM_ECDSAP256SHA256
) {
307 curve
= NID_X9_62_prime256v1
; /* NIST P-256 */
309 } else if (algorithm
== DNSSEC_ALGORITHM_ECDSAP384SHA384
) {
310 curve
= NID_secp384r1
; /* NIST P-384 */
315 if (dnskey
->dnskey
.key_size
!= key_size
* 2)
318 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
321 q
= newa(uint8_t, key_size
*2 + 1);
322 q
[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
323 memcpy(q
+1, dnskey
->dnskey
.key
, key_size
*2);
325 return dnssec_ecdsa_verify_raw(
328 rrsig
->rrsig
.signature
, key_size
,
329 (uint8_t*) rrsig
->rrsig
.signature
+ key_size
, key_size
,
334 static int dnssec_eddsa_verify_raw(
336 const uint8_t *signature
, size_t signature_size
,
337 const uint8_t *data
, size_t data_size
,
338 const uint8_t *key
, size_t key_size
) {
340 _cleanup_(EVP_PKEY_freep
) EVP_PKEY
*evkey
= NULL
;
341 _cleanup_(EVP_PKEY_CTX_freep
) EVP_PKEY_CTX
*pctx
= NULL
;
342 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= NULL
;
345 assert(curve
== NID_ED25519
);
346 assert(signature_size
== key_size
* 2);
348 uint8_t *q
= newa(uint8_t, signature_size
+ 1);
349 q
[0] = 0x04; /* Prepend 0x04 to indicate an uncompressed key */
350 memcpy(q
+1, signature
, signature_size
);
352 evkey
= EVP_PKEY_new_raw_public_key(EVP_PKEY_ED25519
, NULL
, key
, key_size
);
354 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
355 "EVP_PKEY_new_raw_public_key failed: 0x%lx", ERR_get_error());
357 pctx
= EVP_PKEY_CTX_new(evkey
, NULL
);
361 ctx
= EVP_MD_CTX_new();
365 /* This prevents EVP_DigestVerifyInit from managing pctx and complicating our free logic. */
366 EVP_MD_CTX_set_pkey_ctx(ctx
, pctx
);
368 /* One might be tempted to use EVP_PKEY_verify_init, but see Ed25519(7ssl). */
369 if (EVP_DigestVerifyInit(ctx
, &pctx
, NULL
, NULL
, evkey
) <= 0)
372 r
= EVP_DigestVerify(ctx
, signature
, signature_size
, data
, data_size
);
374 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
375 "Signature verification failed: 0x%lx", ERR_get_error());
380 static int dnssec_eddsa_verify(
382 const void *data
, size_t data_size
,
383 DnsResourceRecord
*rrsig
,
384 DnsResourceRecord
*dnskey
) {
388 if (algorithm
== DNSSEC_ALGORITHM_ED25519
) {
394 if (dnskey
->dnskey
.key_size
!= key_size
)
397 if (rrsig
->rrsig
.signature_size
!= key_size
* 2)
400 return dnssec_eddsa_verify_raw(
402 rrsig
->rrsig
.signature
, rrsig
->rrsig
.signature_size
,
404 dnskey
->dnskey
.key
, key_size
);
407 static int md_add_uint8(EVP_MD_CTX
*ctx
, uint8_t v
) {
408 return EVP_DigestUpdate(ctx
, &v
, sizeof(v
));
411 static int md_add_uint16(EVP_MD_CTX
*ctx
, uint16_t v
) {
413 return EVP_DigestUpdate(ctx
, &v
, sizeof(v
));
416 static void fwrite_uint8(FILE *fp
, uint8_t v
) {
417 fwrite(&v
, sizeof(v
), 1, fp
);
420 static void fwrite_uint16(FILE *fp
, uint16_t v
) {
422 fwrite(&v
, sizeof(v
), 1, fp
);
425 static void fwrite_uint32(FILE *fp
, uint32_t v
) {
427 fwrite(&v
, sizeof(v
), 1, fp
);
430 static int dnssec_rrsig_prepare(DnsResourceRecord
*rrsig
) {
431 int n_key_labels
, n_signer_labels
;
435 /* Checks whether the specified RRSIG RR is somewhat valid, and initializes the .n_skip_labels_source
436 * and .n_skip_labels_signer fields so that we can use them later on. */
439 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
441 /* Check if this RRSIG RR is already prepared */
442 if (rrsig
->n_skip_labels_source
!= UINT8_MAX
)
445 if (rrsig
->rrsig
.inception
> rrsig
->rrsig
.expiration
)
448 name
= dns_resource_key_name(rrsig
->key
);
450 n_key_labels
= dns_name_count_labels(name
);
451 if (n_key_labels
< 0)
453 if (rrsig
->rrsig
.labels
> n_key_labels
)
456 n_signer_labels
= dns_name_count_labels(rrsig
->rrsig
.signer
);
457 if (n_signer_labels
< 0)
458 return n_signer_labels
;
459 if (n_signer_labels
> rrsig
->rrsig
.labels
)
462 r
= dns_name_skip(name
, n_key_labels
- n_signer_labels
, &name
);
468 /* Check if the signer is really a suffix of us */
469 r
= dns_name_equal(name
, rrsig
->rrsig
.signer
);
475 assert(n_key_labels
< UINT8_MAX
); /* UINT8_MAX/-1 means unsigned. */
476 rrsig
->n_skip_labels_source
= n_key_labels
- rrsig
->rrsig
.labels
;
477 rrsig
->n_skip_labels_signer
= n_key_labels
- n_signer_labels
;
482 static int dnssec_rrsig_expired(DnsResourceRecord
*rrsig
, usec_t realtime
) {
483 usec_t expiration
, inception
, skew
;
486 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
488 if (realtime
== USEC_INFINITY
)
489 realtime
= now(CLOCK_REALTIME
);
491 expiration
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
492 inception
= rrsig
->rrsig
.inception
* USEC_PER_SEC
;
494 /* Consider inverted validity intervals as expired */
495 if (inception
> expiration
)
498 /* Permit a certain amount of clock skew of 10% of the valid
499 * time range. This takes inspiration from unbound's
501 skew
= (expiration
- inception
) / 10;
505 if (inception
< skew
)
510 if (expiration
+ skew
< expiration
)
511 expiration
= USEC_INFINITY
;
515 return realtime
< inception
|| realtime
> expiration
;
518 static const EVP_MD
* algorithm_to_implementation_id(uint8_t algorithm
) {
520 /* Translates a DNSSEC signature algorithm into an openssl digest identifier.
522 * Note that we implement all algorithms listed as "Must implement" and "Recommended to Implement" in
523 * RFC6944. We don't implement any algorithms that are listed as "Optional" or "Must Not Implement".
524 * Specifically, we do not implement RSAMD5, DSASHA1, DH, DSA-NSEC3-SHA1, and GOST-ECC. */
528 case DNSSEC_ALGORITHM_RSASHA1
:
529 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
532 case DNSSEC_ALGORITHM_RSASHA256
:
533 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
536 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
539 case DNSSEC_ALGORITHM_RSASHA512
:
547 static void dnssec_fix_rrset_ttl(
548 DnsResourceRecord
*list
[],
550 DnsResourceRecord
*rrsig
) {
556 for (unsigned k
= 0; k
< n
; k
++) {
557 DnsResourceRecord
*rr
= list
[k
];
559 /* Pick the TTL as the minimum of the RR's TTL, the
560 * RR's original TTL according to the RRSIG and the
561 * RRSIG's own TTL, see RFC 4035, Section 5.3.3 */
562 rr
->ttl
= MIN3(rr
->ttl
, rrsig
->rrsig
.original_ttl
, rrsig
->ttl
);
563 rr
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
565 /* Copy over information about the signer and wildcard source of synthesis */
566 rr
->n_skip_labels_source
= rrsig
->n_skip_labels_source
;
567 rr
->n_skip_labels_signer
= rrsig
->n_skip_labels_signer
;
570 rrsig
->expiry
= rrsig
->rrsig
.expiration
* USEC_PER_SEC
;
573 static int dnssec_rrset_serialize_sig(
574 DnsResourceRecord
*rrsig
,
576 DnsResourceRecord
**list
,
580 size_t *ret_sig_size
) {
582 _cleanup_(memstream_done
) MemStream m
= {};
583 uint8_t wire_format_name
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
584 DnsResourceRecord
*rr
;
590 assert(list
|| list_len
== 0);
591 assert(ret_sig_data
);
592 assert(ret_sig_size
);
594 f
= memstream_init(&m
);
598 fwrite_uint16(f
, rrsig
->rrsig
.type_covered
);
599 fwrite_uint8(f
, rrsig
->rrsig
.algorithm
);
600 fwrite_uint8(f
, rrsig
->rrsig
.labels
);
601 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
602 fwrite_uint32(f
, rrsig
->rrsig
.expiration
);
603 fwrite_uint32(f
, rrsig
->rrsig
.inception
);
604 fwrite_uint16(f
, rrsig
->rrsig
.key_tag
);
606 r
= dns_name_to_wire_format(rrsig
->rrsig
.signer
, wire_format_name
, sizeof(wire_format_name
), true);
609 fwrite(wire_format_name
, 1, r
, f
);
611 /* Convert the source of synthesis into wire format */
612 r
= dns_name_to_wire_format(source
, wire_format_name
, sizeof(wire_format_name
), true);
616 for (size_t k
= 0; k
< list_len
; k
++) {
621 /* Hash the source of synthesis. If this is a wildcard, then prefix it with the *. label */
623 fwrite((uint8_t[]) { 1, '*'}, sizeof(uint8_t), 2, f
);
624 fwrite(wire_format_name
, 1, r
, f
);
626 fwrite_uint16(f
, rr
->key
->type
);
627 fwrite_uint16(f
, rr
->key
->class);
628 fwrite_uint32(f
, rrsig
->rrsig
.original_ttl
);
630 l
= DNS_RESOURCE_RECORD_RDATA_SIZE(rr
);
633 fwrite_uint16(f
, (uint16_t) l
);
634 fwrite(DNS_RESOURCE_RECORD_RDATA(rr
), 1, l
, f
);
637 return memstream_finalize(&m
, ret_sig_data
, ret_sig_size
);
640 static int dnssec_rrset_verify_sig(
641 DnsResourceRecord
*rrsig
,
642 DnsResourceRecord
*dnskey
,
643 const char *sig_data
,
649 assert(sig_size
> 0);
651 const EVP_MD
*md_algorithm
;
653 uint8_t hash
[EVP_MAX_MD_SIZE
];
656 switch (rrsig
->rrsig
.algorithm
) {
657 case DNSSEC_ALGORITHM_ED25519
:
658 return dnssec_eddsa_verify(
659 rrsig
->rrsig
.algorithm
,
663 case DNSSEC_ALGORITHM_ED448
:
666 /* OK, the RRs are now in canonical order. Let's calculate the digest */
667 md_algorithm
= algorithm_to_implementation_id(rrsig
->rrsig
.algorithm
);
671 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
675 if (EVP_DigestInit_ex(ctx
, md_algorithm
, NULL
) <= 0)
678 if (EVP_DigestUpdate(ctx
, sig_data
, sig_size
) <= 0)
681 if (EVP_DigestFinal_ex(ctx
, hash
, &hash_size
) <= 0)
684 assert(hash_size
> 0);
687 switch (rrsig
->rrsig
.algorithm
) {
689 case DNSSEC_ALGORITHM_RSASHA1
:
690 case DNSSEC_ALGORITHM_RSASHA1_NSEC3_SHA1
:
691 case DNSSEC_ALGORITHM_RSASHA256
:
692 case DNSSEC_ALGORITHM_RSASHA512
:
693 return dnssec_rsa_verify(
699 case DNSSEC_ALGORITHM_ECDSAP256SHA256
:
700 case DNSSEC_ALGORITHM_ECDSAP384SHA384
:
701 return dnssec_ecdsa_verify(
703 rrsig
->rrsig
.algorithm
,
709 assert_not_reached();
713 int dnssec_verify_rrset(
715 const DnsResourceKey
*key
,
716 DnsResourceRecord
*rrsig
,
717 DnsResourceRecord
*dnskey
,
719 DnssecResult
*result
) {
721 DnsResourceRecord
**list
, *rr
;
722 const char *source
, *name
;
723 _cleanup_free_
char *sig_data
= NULL
;
724 size_t sig_size
= 0; /* avoid false maybe-uninitialized warning */
733 assert(rrsig
->key
->type
== DNS_TYPE_RRSIG
);
734 assert(dnskey
->key
->type
== DNS_TYPE_DNSKEY
);
736 /* Verifies that the RRSet matches the specified "key" in "a",
737 * using the signature "rrsig" and the key "dnskey". It's
738 * assumed that RRSIG and DNSKEY match. */
740 r
= dnssec_rrsig_prepare(rrsig
);
742 *result
= DNSSEC_INVALID
;
748 r
= dnssec_rrsig_expired(rrsig
, realtime
);
752 *result
= DNSSEC_SIGNATURE_EXPIRED
;
756 name
= dns_resource_key_name(key
);
758 /* Some keys may only appear signed in the zone apex, and are invalid anywhere else. (SOA, NS...) */
759 if (dns_type_apex_only(rrsig
->rrsig
.type_covered
)) {
760 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
764 *result
= DNSSEC_INVALID
;
769 /* OTOH DS RRs may not appear in the zone apex, but are valid everywhere else. */
770 if (rrsig
->rrsig
.type_covered
== DNS_TYPE_DS
) {
771 r
= dns_name_equal(rrsig
->rrsig
.signer
, name
);
775 *result
= DNSSEC_INVALID
;
780 /* Determine the "Source of Synthesis" and whether this is a wildcard RRSIG */
781 r
= dns_name_suffix(name
, rrsig
->rrsig
.labels
, &source
);
784 if (r
> 0 && !dns_type_may_wildcard(rrsig
->rrsig
.type_covered
)) {
785 /* We refuse to validate NSEC3 or SOA RRs that are synthesized from wildcards */
786 *result
= DNSSEC_INVALID
;
790 /* If we stripped a single label, then let's see if that maybe was "*". If so, we are not really
791 * synthesized from a wildcard, we are the wildcard itself. Treat that like a normal name. */
792 r
= dns_name_startswith(name
, "*");
802 /* Collect all relevant RRs in a single array, so that we can look at the RRset */
803 list
= newa(DnsResourceRecord
*, dns_answer_size(a
));
805 DNS_ANSWER_FOREACH(rr
, a
) {
806 r
= dns_resource_key_equal(key
, rr
->key
);
812 /* We need the wire format for ordering, and digest calculation */
813 r
= dns_resource_record_to_wire_format(rr
, true);
819 if (n
> VERIFY_RRS_MAX
)
826 /* Bring the RRs into canonical order */
827 typesafe_qsort(list
, n
, rr_compare
);
829 r
= dnssec_rrset_serialize_sig(rrsig
, source
, list
, n
, wildcard
,
830 &sig_data
, &sig_size
);
834 r
= dnssec_rrset_verify_sig(rrsig
, dnskey
, sig_data
, sig_size
);
835 if (r
== -EOPNOTSUPP
) {
836 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
842 /* Now, fix the ttl, expiry, and remember the synthesizing source and the signer */
844 dnssec_fix_rrset_ttl(list
, n
, rrsig
);
847 *result
= DNSSEC_INVALID
;
849 *result
= DNSSEC_VALIDATED_WILDCARD
;
851 *result
= DNSSEC_VALIDATED
;
856 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
861 /* Checks if the specified DNSKEY RR matches the key used for
862 * the signature in the specified RRSIG RR */
864 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
867 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
869 if (dnskey
->key
->class != rrsig
->key
->class)
871 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
873 if (!revoked_ok
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
875 if (dnskey
->dnskey
.protocol
!= 3)
877 if (dnskey
->dnskey
.algorithm
!= rrsig
->rrsig
.algorithm
)
880 if (dnssec_keytag(dnskey
, false) != rrsig
->rrsig
.key_tag
)
883 return dns_name_equal(dns_resource_key_name(dnskey
->key
), rrsig
->rrsig
.signer
);
886 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
890 /* Checks if the specified RRSIG RR protects the RRSet of the specified RR key. */
892 if (rrsig
->key
->type
!= DNS_TYPE_RRSIG
)
894 if (rrsig
->key
->class != key
->class)
896 if (rrsig
->rrsig
.type_covered
!= key
->type
)
899 return dns_name_equal(dns_resource_key_name(rrsig
->key
), dns_resource_key_name(key
));
902 int dnssec_verify_rrset_search(
904 const DnsResourceKey
*key
,
905 DnsAnswer
*validated_dnskeys
,
907 DnssecResult
*result
,
908 DnsResourceRecord
**ret_rrsig
) {
910 bool found_rrsig
= false, found_invalid
= false, found_expired_rrsig
= false, found_unsupported_algorithm
= false;
911 unsigned nvalidations
= 0;
912 DnsResourceRecord
*rrsig
;
918 /* Verifies all RRs from "a" that match the key "key" against DNSKEYs in "validated_dnskeys" */
920 if (dns_answer_isempty(a
))
923 /* Iterate through each RRSIG RR. */
924 DNS_ANSWER_FOREACH(rrsig
, a
) {
925 DnsResourceRecord
*dnskey
;
926 DnsAnswerFlags flags
;
928 /* Is this an RRSIG RR that applies to RRs matching our key? */
929 r
= dnssec_key_match_rrsig(key
, rrsig
);
937 /* Look for a matching key */
938 DNS_ANSWER_FOREACH_FLAGS(dnskey
, flags
, validated_dnskeys
) {
939 DnssecResult one_result
;
941 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
944 /* Is this a DNSKEY RR that matches they key of our RRSIG? */
945 r
= dnssec_rrsig_match_dnskey(rrsig
, dnskey
, false);
951 /* Take the time here, if it isn't set yet, so
952 * that we do all validations with the same
954 if (realtime
== USEC_INFINITY
)
955 realtime
= now(CLOCK_REALTIME
);
957 /* Have we seen an unreasonable number of invalid signatures? */
958 if (nvalidations
> DNSSEC_INVALID_MAX
) {
961 *result
= DNSSEC_TOO_MANY_VALIDATIONS
;
962 return (int) nvalidations
;
965 /* Yay, we found a matching RRSIG with a matching
966 * DNSKEY, awesome. Now let's verify all entries of
967 * the RRSet against the RRSIG and DNSKEY
970 r
= dnssec_verify_rrset(a
, key
, rrsig
, dnskey
, realtime
, &one_result
);
976 switch (one_result
) {
978 case DNSSEC_VALIDATED
:
979 case DNSSEC_VALIDATED_WILDCARD
:
980 /* Yay, the RR has been validated,
981 * return immediately, but fix up the expiry */
985 *result
= one_result
;
986 return (int) nvalidations
;
989 /* If the signature is invalid, let's try another
990 key and/or signature. After all they
991 key_tags and stuff are not unique, and
992 might be shared by multiple keys. */
993 found_invalid
= true;
996 case DNSSEC_UNSUPPORTED_ALGORITHM
:
997 /* If the key algorithm is
998 unsupported, try another
999 RRSIG/DNSKEY pair, but remember we
1000 encountered this, so that we can
1001 return a proper error when we
1002 encounter nothing better. */
1003 found_unsupported_algorithm
= true;
1006 case DNSSEC_SIGNATURE_EXPIRED
:
1007 /* If the signature is expired, try
1008 another one, but remember it, so
1009 that we can return this */
1010 found_expired_rrsig
= true;
1014 assert_not_reached();
1019 if (found_expired_rrsig
)
1020 *result
= DNSSEC_SIGNATURE_EXPIRED
;
1021 else if (found_unsupported_algorithm
)
1022 *result
= DNSSEC_UNSUPPORTED_ALGORITHM
;
1023 else if (found_invalid
)
1024 *result
= DNSSEC_INVALID
;
1025 else if (found_rrsig
)
1026 *result
= DNSSEC_MISSING_KEY
;
1028 *result
= DNSSEC_NO_SIGNATURE
;
1033 return (int) nvalidations
;
1036 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
1037 DnsResourceRecord
*rr
;
1040 /* Checks whether there's at least one RRSIG in 'a' that protects RRs of the specified key */
1042 DNS_ANSWER_FOREACH(rr
, a
) {
1043 r
= dnssec_key_match_rrsig(key
, rr
);
1053 static const EVP_MD
* digest_to_hash_md(uint8_t algorithm
) {
1055 /* Translates a DNSSEC digest algorithm into an openssl digest identifier */
1057 switch (algorithm
) {
1059 case DNSSEC_DIGEST_SHA1
:
1062 case DNSSEC_DIGEST_SHA256
:
1063 return EVP_sha256();
1065 case DNSSEC_DIGEST_SHA384
:
1066 return EVP_sha384();
1073 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
1074 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1075 size_t encoded_length
;
1081 /* Implements DNSKEY verification by a DS, according to RFC 4035, section 5.2 */
1083 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1085 if (ds
->key
->type
!= DNS_TYPE_DS
)
1087 if ((dnskey
->dnskey
.flags
& DNSKEY_FLAG_ZONE_KEY
) == 0)
1088 return -EKEYREJECTED
;
1089 if (!mask_revoke
&& (dnskey
->dnskey
.flags
& DNSKEY_FLAG_REVOKE
))
1090 return -EKEYREJECTED
;
1091 if (dnskey
->dnskey
.protocol
!= 3)
1092 return -EKEYREJECTED
;
1094 if (dnskey
->dnskey
.algorithm
!= ds
->ds
.algorithm
)
1096 if (dnssec_keytag(dnskey
, mask_revoke
) != ds
->ds
.key_tag
)
1099 r
= dns_name_to_wire_format(dns_resource_key_name(dnskey
->key
), wire_format
, sizeof wire_format
, true);
1104 const EVP_MD
*md_algorithm
= digest_to_hash_md(ds
->ds
.digest_type
);
1108 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= NULL
;
1109 uint8_t result
[EVP_MAX_MD_SIZE
];
1111 unsigned hash_size
= EVP_MD_size(md_algorithm
);
1112 assert(hash_size
> 0);
1114 if (ds
->ds
.digest_size
!= hash_size
)
1117 ctx
= EVP_MD_CTX_new();
1121 if (EVP_DigestInit_ex(ctx
, md_algorithm
, NULL
) <= 0)
1124 if (EVP_DigestUpdate(ctx
, wire_format
, encoded_length
) <= 0)
1128 md_add_uint16(ctx
, dnskey
->dnskey
.flags
& ~DNSKEY_FLAG_REVOKE
);
1130 md_add_uint16(ctx
, dnskey
->dnskey
.flags
);
1132 r
= md_add_uint8(ctx
, dnskey
->dnskey
.protocol
);
1135 r
= md_add_uint8(ctx
, dnskey
->dnskey
.algorithm
);
1138 if (EVP_DigestUpdate(ctx
, dnskey
->dnskey
.key
, dnskey
->dnskey
.key_size
) <= 0)
1141 if (EVP_DigestFinal_ex(ctx
, result
, NULL
) <= 0)
1144 return memcmp(result
, ds
->ds
.digest
, ds
->ds
.digest_size
) == 0;
1147 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
1148 DnsResourceRecord
*ds
;
1149 DnsAnswerFlags flags
;
1154 if (dnskey
->key
->type
!= DNS_TYPE_DNSKEY
)
1157 DNS_ANSWER_FOREACH_FLAGS(ds
, flags
, validated_ds
) {
1159 if ((flags
& DNS_ANSWER_AUTHENTICATED
) == 0)
1162 if (ds
->key
->type
!= DNS_TYPE_DS
)
1164 if (ds
->key
->class != dnskey
->key
->class)
1167 r
= dns_name_equal(dns_resource_key_name(dnskey
->key
), dns_resource_key_name(ds
->key
));
1173 r
= dnssec_verify_dnskey_by_ds(dnskey
, ds
, false);
1174 if (IN_SET(r
, -EKEYREJECTED
, -EOPNOTSUPP
))
1175 continue; /* The DNSKEY is revoked or otherwise invalid, or we don't support the digest algorithm */
1185 static const EVP_MD
* nsec3_hash_to_hash_md(uint8_t algorithm
) {
1187 /* Translates a DNSSEC NSEC3 hash algorithm into an openssl digest identifier */
1189 switch (algorithm
) {
1191 case NSEC3_ALGORITHM_SHA1
:
1199 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
1200 uint8_t wire_format
[DNS_WIRE_FORMAT_HOSTNAME_MAX
];
1207 if (nsec3
->key
->type
!= DNS_TYPE_NSEC3
)
1210 if (nsec3
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1211 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
),
1212 "Ignoring NSEC3 RR %s with excessive number of iterations.",
1213 dns_resource_record_to_string(nsec3
));
1215 const EVP_MD
*algorithm
= nsec3_hash_to_hash_md(nsec3
->nsec3
.algorithm
);
1219 size_t hash_size
= EVP_MD_size(algorithm
);
1220 assert(hash_size
> 0);
1222 if (nsec3
->nsec3
.next_hashed_name_size
!= hash_size
)
1225 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
1229 if (EVP_DigestInit_ex(ctx
, algorithm
, NULL
) <= 0)
1232 r
= dns_name_to_wire_format(name
, wire_format
, sizeof(wire_format
), true);
1236 if (EVP_DigestUpdate(ctx
, wire_format
, r
) <= 0)
1238 if (EVP_DigestUpdate(ctx
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
) <= 0)
1241 uint8_t result
[EVP_MAX_MD_SIZE
];
1242 if (EVP_DigestFinal_ex(ctx
, result
, NULL
) <= 0)
1245 for (unsigned k
= 0; k
< nsec3
->nsec3
.iterations
; k
++) {
1246 if (EVP_DigestInit_ex(ctx
, algorithm
, NULL
) <= 0)
1248 if (EVP_DigestUpdate(ctx
, result
, hash_size
) <= 0)
1250 if (EVP_DigestUpdate(ctx
, nsec3
->nsec3
.salt
, nsec3
->nsec3
.salt_size
) <= 0)
1253 if (EVP_DigestFinal_ex(ctx
, result
, NULL
) <= 0)
1257 memcpy(ret
, result
, hash_size
);
1258 return (int) hash_size
;
1261 static int nsec3_is_good(DnsResourceRecord
*rr
, DnsResourceRecord
*nsec3
) {
1267 if (rr
->key
->type
!= DNS_TYPE_NSEC3
)
1270 /* RFC 5155, Section 8.2 says we MUST ignore NSEC3 RRs with flags != 0 or 1 */
1271 if (!IN_SET(rr
->nsec3
.flags
, 0, 1))
1274 /* Ignore NSEC3 RRs whose algorithm we don't know */
1275 if (!nsec3_hash_to_hash_md(rr
->nsec3
.algorithm
))
1278 /* Ignore NSEC3 RRs with an excessive number of required iterations */
1279 if (rr
->nsec3
.iterations
> NSEC3_ITERATIONS_MAX
)
1282 /* Ignore NSEC3 RRs generated from wildcards. If these NSEC3 RRs weren't correctly signed we can't make this
1283 * check (since rr->n_skip_labels_source is -1), but that's OK, as we won't trust them anyway in that case. */
1284 if (!IN_SET(rr
->n_skip_labels_source
, 0, UINT8_MAX
))
1286 /* Ignore NSEC3 RRs that are located anywhere else than one label below the zone */
1287 if (!IN_SET(rr
->n_skip_labels_signer
, 1, UINT8_MAX
))
1293 /* If a second NSEC3 RR is specified, also check if they are from the same zone. */
1295 if (nsec3
== rr
) /* Shortcut */
1298 if (rr
->key
->class != nsec3
->key
->class)
1300 if (rr
->nsec3
.algorithm
!= nsec3
->nsec3
.algorithm
)
1302 if (rr
->nsec3
.iterations
!= nsec3
->nsec3
.iterations
)
1304 if (rr
->nsec3
.salt_size
!= nsec3
->nsec3
.salt_size
)
1306 if (memcmp_safe(rr
->nsec3
.salt
, nsec3
->nsec3
.salt
, rr
->nsec3
.salt_size
) != 0)
1309 a
= dns_resource_key_name(rr
->key
);
1310 r
= dns_name_parent(&a
); /* strip off hash */
1314 b
= dns_resource_key_name(nsec3
->key
);
1315 r
= dns_name_parent(&b
); /* strip off hash */
1319 /* Make sure both have the same parent */
1320 return dns_name_equal(a
, b
);
1323 static int nsec3_hashed_domain_format(const uint8_t *hashed
, size_t hashed_size
, const char *zone
, char **ret
) {
1324 _cleanup_free_
char *l
= NULL
;
1328 assert(hashed_size
> 0);
1332 l
= base32hexmem(hashed
, hashed_size
, false);
1336 j
= strjoin(l
, ".", zone
);
1341 return (int) hashed_size
;
1344 static int nsec3_hashed_domain_make(DnsResourceRecord
*nsec3
, const char *domain
, const char *zone
, char **ret
) {
1345 uint8_t hashed
[DNSSEC_HASH_SIZE_MAX
];
1353 hashed_size
= dnssec_nsec3_hash(nsec3
, domain
, hashed
);
1354 if (hashed_size
< 0)
1357 return nsec3_hashed_domain_format(hashed
, (size_t) hashed_size
, zone
, ret
);
1360 /* See RFC 5155, Section 8
1361 * First try to find a NSEC3 record that matches our query precisely, if that fails, find the closest
1362 * enclosure. Secondly, find a proof that there is no closer enclosure and either a proof that there
1363 * is no wildcard domain as a direct descendant of the closest enclosure, or find an NSEC3 record that
1364 * matches the wildcard domain.
1366 * Based on this we can prove either the existence of the record in @key, or NXDOMAIN or NODATA, or
1367 * that there is no proof either way. The latter is the case if a proof of non-existence of a given
1368 * name uses an NSEC3 record with the opt-out bit set. Lastly, if we are given insufficient NSEC3 records
1369 * to conclude anything we indicate this by returning NO_RR. */
1370 static int dnssec_test_nsec3(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1371 _cleanup_free_
char *next_closer_domain
= NULL
, *wildcard_domain
= NULL
;
1372 const char *zone
, *p
, *pp
= NULL
, *wildcard
;
1373 DnsResourceRecord
*rr
, *enclosure_rr
, *zone_rr
, *wildcard_rr
= NULL
;
1374 DnsAnswerFlags flags
;
1376 bool a
, no_closer
= false, no_wildcard
= false, optout
= false;
1381 /* First step, find the zone name and the NSEC3 parameters of the zone.
1382 * it is sufficient to look for the longest common suffix we find with
1383 * any NSEC3 RR in the response. Any NSEC3 record will do as all NSEC3
1384 * records from a given zone in a response must use the same
1386 zone
= dns_resource_key_name(key
);
1388 DNS_ANSWER_FOREACH_FLAGS(zone_rr
, flags
, answer
) {
1389 r
= nsec3_is_good(zone_rr
, NULL
);
1395 r
= dns_name_equal_skip(dns_resource_key_name(zone_rr
->key
), 1, zone
);
1402 /* Strip one label from the front */
1403 r
= dns_name_parent(&zone
);
1410 *result
= DNSSEC_NSEC_NO_RR
;
1414 /* Second step, find the closest encloser NSEC3 RR in 'answer' that matches 'key' */
1415 p
= dns_resource_key_name(key
);
1417 _cleanup_free_
char *hashed_domain
= NULL
;
1419 hashed_size
= nsec3_hashed_domain_make(zone_rr
, p
, zone
, &hashed_domain
);
1420 if (hashed_size
== -EOPNOTSUPP
) {
1421 *result
= DNSSEC_NSEC_UNSUPPORTED_ALGORITHM
;
1424 if (hashed_size
< 0)
1427 DNS_ANSWER_FOREACH_FLAGS(enclosure_rr
, flags
, answer
) {
1429 r
= nsec3_is_good(enclosure_rr
, zone_rr
);
1435 if (enclosure_rr
->nsec3
.next_hashed_name_size
!= (size_t) hashed_size
)
1438 r
= dns_name_equal(dns_resource_key_name(enclosure_rr
->key
), hashed_domain
);
1442 a
= flags
& DNS_ANSWER_AUTHENTICATED
;
1443 goto found_closest_encloser
;
1447 /* We didn't find the closest encloser with this name,
1448 * but let's remember this domain name, it might be
1449 * the next closer name */
1453 /* Strip one label from the front */
1454 r
= dns_name_parent(&p
);
1461 *result
= DNSSEC_NSEC_NO_RR
;
1464 found_closest_encloser
:
1465 /* We found a closest encloser in 'p'; next closer is 'pp' */
1468 /* We have an exact match! If we area looking for a DS RR, then we must insist that we got the NSEC3 RR
1469 * from the parent. Otherwise the one from the child. Do so, by checking whether SOA and NS are
1470 * appropriately set. */
1472 if (key
->type
== DNS_TYPE_DS
) {
1473 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1476 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1477 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1481 /* No next closer NSEC3 RR. That means there's a direct NSEC3 RR for our key. */
1482 if (bitmap_isset(enclosure_rr
->nsec3
.types
, key
->type
))
1483 *result
= DNSSEC_NSEC_FOUND
;
1484 else if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1485 *result
= DNSSEC_NSEC_CNAME
;
1487 *result
= DNSSEC_NSEC_NODATA
;
1492 *ttl
= enclosure_rr
->ttl
;
1497 /* Ensure this is not a DNAME domain, see RFC5155, section 8.3. */
1498 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_DNAME
))
1501 /* Ensure that this data is from the delegated domain
1502 * (i.e. originates from the "lower" DNS server), and isn't
1503 * just glue records (i.e. doesn't originate from the "upper"
1505 if (bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_NS
) &&
1506 !bitmap_isset(enclosure_rr
->nsec3
.types
, DNS_TYPE_SOA
))
1509 /* Prove that there is no next closer and whether or not there is a wildcard domain. */
1511 wildcard
= strjoina("*.", p
);
1512 r
= nsec3_hashed_domain_make(enclosure_rr
, wildcard
, zone
, &wildcard_domain
);
1515 if (r
!= hashed_size
)
1518 r
= nsec3_hashed_domain_make(enclosure_rr
, pp
, zone
, &next_closer_domain
);
1521 if (r
!= hashed_size
)
1524 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1525 _cleanup_free_
char *next_hashed_domain
= NULL
;
1527 r
= nsec3_is_good(rr
, zone_rr
);
1533 r
= nsec3_hashed_domain_format(rr
->nsec3
.next_hashed_name
, rr
->nsec3
.next_hashed_name_size
, zone
, &next_hashed_domain
);
1537 r
= dns_name_between(dns_resource_key_name(rr
->key
), next_closer_domain
, next_hashed_domain
);
1541 if (rr
->nsec3
.flags
& 1)
1544 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1549 r
= dns_name_equal(dns_resource_key_name(rr
->key
), wildcard_domain
);
1553 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1558 r
= dns_name_between(dns_resource_key_name(rr
->key
), wildcard_domain
, next_hashed_domain
);
1562 if (rr
->nsec3
.flags
& 1)
1563 /* This only makes sense if we have a wildcard delegation, which is
1564 * very unlikely, see RFC 4592, Section 4.2, but we cannot rely on
1565 * this not happening, so hence cannot simply conclude NXDOMAIN as
1569 a
= a
&& (flags
& DNS_ANSWER_AUTHENTICATED
);
1575 if (wildcard_rr
&& no_wildcard
)
1579 *result
= DNSSEC_NSEC_NO_RR
;
1584 /* A wildcard exists that matches our query. */
1586 /* This is not specified in any RFC to the best of my knowledge, but
1587 * if the next closer enclosure is covered by an opt-out NSEC3 RR
1588 * it means that we cannot prove that the source of synthesis is
1589 * correct, as there may be a closer match. */
1590 *result
= DNSSEC_NSEC_OPTOUT
;
1591 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, key
->type
))
1592 *result
= DNSSEC_NSEC_FOUND
;
1593 else if (bitmap_isset(wildcard_rr
->nsec3
.types
, DNS_TYPE_CNAME
))
1594 *result
= DNSSEC_NSEC_CNAME
;
1596 *result
= DNSSEC_NSEC_NODATA
;
1599 /* The RFC only specifies that we have to care for optout for NODATA for
1600 * DS records. However, children of an insecure opt-out delegation should
1601 * also be considered opt-out, rather than verified NXDOMAIN.
1602 * Note that we do not require a proof of wildcard non-existence if the
1603 * next closer domain is covered by an opt-out, as that would not provide
1604 * any additional information. */
1605 *result
= DNSSEC_NSEC_OPTOUT
;
1606 else if (no_wildcard
)
1607 *result
= DNSSEC_NSEC_NXDOMAIN
;
1609 *result
= DNSSEC_NSEC_NO_RR
;
1619 *ttl
= enclosure_rr
->ttl
;
1624 static int dnssec_nsec_wildcard_equal(DnsResourceRecord
*rr
, const char *name
) {
1625 char label
[DNS_LABEL_MAX
+1];
1630 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1632 /* Checks whether the specified RR has a name beginning in "*.", and if the rest is a suffix of our name */
1634 if (rr
->n_skip_labels_source
!= 1)
1637 n
= dns_resource_key_name(rr
->key
);
1638 r
= dns_label_unescape(&n
, label
, sizeof label
, 0);
1641 if (r
!= 1 || label
[0] != '*')
1644 return dns_name_endswith(name
, n
);
1647 static int dnssec_nsec_in_path(DnsResourceRecord
*rr
, const char *name
) {
1648 const char *nn
, *common_suffix
;
1652 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1654 /* Checks whether the specified nsec RR indicates that name is an empty non-terminal (ENT)
1656 * A couple of examples:
1658 * NSEC bar → waldo.foo.bar: indicates that foo.bar exists and is an ENT
1659 * NSEC waldo.foo.bar → yyy.zzz.xoo.bar: indicates that xoo.bar and zzz.xoo.bar exist and are ENTs
1660 * NSEC yyy.zzz.xoo.bar → bar: indicates pretty much nothing about ENTs
1663 /* First, determine parent of next domain. */
1664 nn
= rr
->nsec
.next_domain_name
;
1665 r
= dns_name_parent(&nn
);
1669 /* If the name we just determined is not equal or child of the name we are interested in, then we can't say
1670 * anything at all. */
1671 r
= dns_name_endswith(nn
, name
);
1675 /* 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. */
1676 r
= dns_name_common_suffix(dns_resource_key_name(rr
->key
), rr
->nsec
.next_domain_name
, &common_suffix
);
1680 return dns_name_endswith(name
, common_suffix
);
1683 static int dnssec_nsec_from_parent_zone(DnsResourceRecord
*rr
, const char *name
) {
1687 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1689 /* Checks whether this NSEC originates to the parent zone or the child zone. */
1691 r
= dns_name_parent(&name
);
1695 r
= dns_name_equal(name
, dns_resource_key_name(rr
->key
));
1699 /* DNAME, and NS without SOA is an indication for a delegation. */
1700 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_DNAME
))
1703 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) && !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1709 static int dnssec_nsec_covers(DnsResourceRecord
*rr
, const char *name
) {
1714 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1716 /* Checks whether the name is covered by this NSEC RR. This means, that the name is somewhere below the NSEC's
1717 * signer name, and between the NSEC's two names. */
1719 r
= dns_resource_record_signer(rr
, &signer
);
1723 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
1727 return dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
1730 static int dnssec_nsec_generate_wildcard(DnsResourceRecord
*rr
, const char *name
, char **wc
) {
1731 const char *common_suffix1
, *common_suffix2
, *signer
;
1732 int r
, labels1
, labels2
;
1735 assert(rr
->key
->type
== DNS_TYPE_NSEC
);
1737 /* Generates "Wildcard at the Closest Encloser" for the given name and NSEC RR. */
1739 r
= dns_resource_record_signer(rr
, &signer
);
1743 r
= dns_name_endswith(name
, signer
); /* this NSEC isn't suitable the name is not in the signer's domain */
1747 r
= dns_name_common_suffix(name
, dns_resource_key_name(rr
->key
), &common_suffix1
);
1751 r
= dns_name_common_suffix(name
, rr
->nsec
.next_domain_name
, &common_suffix2
);
1755 labels1
= dns_name_count_labels(common_suffix1
);
1759 labels2
= dns_name_count_labels(common_suffix2
);
1763 if (labels1
> labels2
)
1764 r
= dns_name_concat("*", common_suffix1
, 0, wc
);
1766 r
= dns_name_concat("*", common_suffix2
, 0, wc
);
1774 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
1775 bool have_nsec3
= false, covering_rr_authenticated
= false, wildcard_rr_authenticated
= false;
1776 DnsResourceRecord
*rr
, *covering_rr
= NULL
, *wildcard_rr
= NULL
;
1777 DnsAnswerFlags flags
;
1784 /* Look for any NSEC/NSEC3 RRs that say something about the specified key. */
1786 name
= dns_resource_key_name(key
);
1788 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1790 if (rr
->key
->class != key
->class)
1793 have_nsec3
= have_nsec3
|| (rr
->key
->type
== DNS_TYPE_NSEC3
);
1795 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
1798 /* The following checks only make sense for NSEC RRs that are not expanded from a wildcard */
1799 r
= dns_resource_record_is_synthetic(rr
);
1800 if (r
== -ENODATA
) /* No signing RR known. */
1807 /* Check if this is a direct match. If so, we have encountered a NODATA case */
1808 r
= dns_name_equal(dns_resource_key_name(rr
->key
), name
);
1812 /* If it's not a direct match, maybe it's a wild card match? */
1813 r
= dnssec_nsec_wildcard_equal(rr
, name
);
1818 if (key
->type
== DNS_TYPE_DS
) {
1819 /* If we look for a DS RR and the server sent us the NSEC RR of the child zone
1820 * we have a problem. For DS RRs we want the NSEC RR from the parent */
1821 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1824 /* For all RR types, ensure that if NS is set SOA is set too, so that we know
1825 * we got the child's NSEC. */
1826 if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_NS
) &&
1827 !bitmap_isset(rr
->nsec
.types
, DNS_TYPE_SOA
))
1831 if (bitmap_isset(rr
->nsec
.types
, key
->type
))
1832 *result
= DNSSEC_NSEC_FOUND
;
1833 else if (bitmap_isset(rr
->nsec
.types
, DNS_TYPE_CNAME
))
1834 *result
= DNSSEC_NSEC_CNAME
;
1836 *result
= DNSSEC_NSEC_NODATA
;
1839 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1846 /* Check if the name we are looking for is an empty non-terminal within the owner or next name
1847 * of the NSEC RR. */
1848 r
= dnssec_nsec_in_path(rr
, name
);
1852 *result
= DNSSEC_NSEC_NODATA
;
1855 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1862 /* The following two "covering" checks, are not useful if the NSEC is from the parent */
1863 r
= dnssec_nsec_from_parent_zone(rr
, name
);
1869 /* Check if this NSEC RR proves the absence of an explicit RR under this name */
1870 r
= dnssec_nsec_covers(rr
, name
);
1873 if (r
> 0 && (!covering_rr
|| !covering_rr_authenticated
)) {
1875 covering_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1880 _cleanup_free_
char *wc
= NULL
;
1881 r
= dnssec_nsec_generate_wildcard(covering_rr
, name
, &wc
);
1885 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1887 if (rr
->key
->class != key
->class)
1890 if (rr
->key
->type
!= DNS_TYPE_NSEC
)
1893 /* Check if this NSEC RR proves the nonexistence of the wildcard */
1894 r
= dnssec_nsec_covers(rr
, wc
);
1897 if (r
> 0 && (!wildcard_rr
|| !wildcard_rr_authenticated
)) {
1899 wildcard_rr_authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
1904 if (covering_rr
&& wildcard_rr
) {
1905 /* If we could prove that neither the name itself, nor the wildcard at the closest encloser exists, we
1906 * proved the NXDOMAIN case. */
1907 *result
= DNSSEC_NSEC_NXDOMAIN
;
1910 *authenticated
= covering_rr_authenticated
&& wildcard_rr_authenticated
;
1912 *ttl
= MIN(covering_rr
->ttl
, wildcard_rr
->ttl
);
1917 /* OK, this was not sufficient. Let's see if NSEC3 can help. */
1919 return dnssec_test_nsec3(answer
, key
, result
, authenticated
, ttl
);
1921 /* No appropriate NSEC RR found, report this. */
1922 *result
= DNSSEC_NSEC_NO_RR
;
1926 static int dnssec_nsec_test_enclosed(DnsAnswer
*answer
, uint16_t type
, const char *name
, const char *zone
, bool *authenticated
) {
1927 DnsResourceRecord
*rr
;
1928 DnsAnswerFlags flags
;
1934 /* Checks whether there's an NSEC/NSEC3 that proves that the specified 'name' is non-existing in the specified
1935 * 'zone'. The 'zone' must be a suffix of the 'name'. */
1937 DNS_ANSWER_FOREACH_FLAGS(rr
, flags
, answer
) {
1940 if (rr
->key
->type
!= type
&& type
!= DNS_TYPE_ANY
)
1943 switch (rr
->key
->type
) {
1947 /* We only care for NSEC RRs from the indicated zone */
1948 r
= dns_resource_record_is_signer(rr
, zone
);
1954 r
= dns_name_between(dns_resource_key_name(rr
->key
), name
, rr
->nsec
.next_domain_name
);
1961 case DNS_TYPE_NSEC3
: {
1962 _cleanup_free_
char *hashed_domain
= NULL
, *next_hashed_domain
= NULL
;
1964 /* We only care for NSEC3 RRs from the indicated zone */
1965 r
= dns_resource_record_is_signer(rr
, zone
);
1971 r
= nsec3_is_good(rr
, NULL
);
1977 /* Format the domain we are testing with the NSEC3 RR's hash function */
1978 r
= nsec3_hashed_domain_make(
1985 if ((size_t) r
!= rr
->nsec3
.next_hashed_name_size
)
1988 /* Format the NSEC3's next hashed name as proper domain name */
1989 r
= nsec3_hashed_domain_format(
1990 rr
->nsec3
.next_hashed_name
,
1991 rr
->nsec3
.next_hashed_name_size
,
1993 &next_hashed_domain
);
1997 r
= dns_name_between(dns_resource_key_name(rr
->key
), hashed_domain
, next_hashed_domain
);
2011 *authenticated
= flags
& DNS_ANSWER_AUTHENTICATED
;
2019 static int dnssec_test_positive_wildcard_nsec3(
2024 bool *authenticated
) {
2026 const char *next_closer
= NULL
;
2029 /* Run a positive NSEC3 wildcard proof. Specifically:
2031 * A proof that the "next closer" of the generating wildcard does not exist.
2033 * Note a key difference between the NSEC3 and NSEC versions of the proof. NSEC RRs don't have to exist for
2034 * empty non-transients. NSEC3 RRs however have to. This means it's sufficient to check if the next closer name
2035 * exists for the NSEC3 RR and we are done.
2037 * 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
2038 * c.d.e.f does not exist. */
2042 r
= dns_name_parent(&name
);
2046 r
= dns_name_equal(name
, source
);
2053 return dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC3
, next_closer
, zone
, authenticated
);
2056 static int dnssec_test_positive_wildcard_nsec(
2061 bool *_authenticated
) {
2063 bool authenticated
= true;
2066 /* Run a positive NSEC wildcard proof. Specifically:
2068 * A proof that there's neither a wildcard name nor a non-wildcard name that is a suffix of the name "name" and
2069 * a prefix of the synthesizing source "source" in the zone "zone".
2071 * See RFC 5155, Section 8.8 and RFC 4035, Section 5.3.4
2073 * 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
2074 * have to prove that none of the following exist:
2084 _cleanup_free_
char *wc
= NULL
;
2087 /* Check if there's an NSEC or NSEC3 RR that proves that the mame we determined is really non-existing,
2088 * i.e between the owner name and the next name of an NSEC RR. */
2089 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, name
, zone
, &a
);
2093 authenticated
= authenticated
&& a
;
2095 /* Strip one label off */
2096 r
= dns_name_parent(&name
);
2100 /* Did we reach the source of synthesis? */
2101 r
= dns_name_equal(name
, source
);
2105 /* Successful exit */
2106 *_authenticated
= authenticated
;
2110 /* Safety check, that the source of synthesis is still our suffix */
2111 r
= dns_name_endswith(name
, source
);
2117 /* Replace the label we stripped off with an asterisk */
2118 wc
= strjoin("*.", name
);
2122 /* And check if the proof holds for the asterisk name, too */
2123 r
= dnssec_nsec_test_enclosed(answer
, DNS_TYPE_NSEC
, wc
, zone
, &a
);
2127 authenticated
= authenticated
&& a
;
2128 /* In the next iteration we'll check the non-asterisk-prefixed version */
2132 int dnssec_test_positive_wildcard(
2137 bool *authenticated
) {
2144 assert(authenticated
);
2146 r
= dns_answer_contains_zone_nsec3(answer
, zone
);
2150 return dnssec_test_positive_wildcard_nsec3(answer
, name
, source
, zone
, authenticated
);
2152 return dnssec_test_positive_wildcard_nsec(answer
, name
, source
, zone
, authenticated
);
2157 int dnssec_verify_rrset(
2159 const DnsResourceKey
*key
,
2160 DnsResourceRecord
*rrsig
,
2161 DnsResourceRecord
*dnskey
,
2163 DnssecResult
*result
) {
2168 int dnssec_rrsig_match_dnskey(DnsResourceRecord
*rrsig
, DnsResourceRecord
*dnskey
, bool revoked_ok
) {
2173 int dnssec_key_match_rrsig(const DnsResourceKey
*key
, DnsResourceRecord
*rrsig
) {
2178 int dnssec_verify_rrset_search(
2180 const DnsResourceKey
*key
,
2181 DnsAnswer
*validated_dnskeys
,
2183 DnssecResult
*result
,
2184 DnsResourceRecord
**ret_rrsig
) {
2189 int dnssec_has_rrsig(DnsAnswer
*a
, const DnsResourceKey
*key
) {
2194 int dnssec_verify_dnskey_by_ds(DnsResourceRecord
*dnskey
, DnsResourceRecord
*ds
, bool mask_revoke
) {
2199 int dnssec_verify_dnskey_by_ds_search(DnsResourceRecord
*dnskey
, DnsAnswer
*validated_ds
) {
2204 int dnssec_nsec3_hash(DnsResourceRecord
*nsec3
, const char *name
, void *ret
) {
2209 int dnssec_nsec_test(DnsAnswer
*answer
, DnsResourceKey
*key
, DnssecNsecResult
*result
, bool *authenticated
, uint32_t *ttl
) {
2214 int dnssec_test_positive_wildcard(
2219 bool *authenticated
) {
2226 static const char* const dnssec_result_table
[_DNSSEC_RESULT_MAX
] = {
2227 [DNSSEC_VALIDATED
] = "validated",
2228 [DNSSEC_VALIDATED_WILDCARD
] = "validated-wildcard",
2229 [DNSSEC_INVALID
] = "invalid",
2230 [DNSSEC_SIGNATURE_EXPIRED
] = "signature-expired",
2231 [DNSSEC_UNSUPPORTED_ALGORITHM
] = "unsupported-algorithm",
2232 [DNSSEC_NO_SIGNATURE
] = "no-signature",
2233 [DNSSEC_MISSING_KEY
] = "missing-key",
2234 [DNSSEC_UNSIGNED
] = "unsigned",
2235 [DNSSEC_FAILED_AUXILIARY
] = "failed-auxiliary",
2236 [DNSSEC_NSEC_MISMATCH
] = "nsec-mismatch",
2237 [DNSSEC_INCOMPATIBLE_SERVER
] = "incompatible-server",
2238 [DNSSEC_UPSTREAM_FAILURE
] = "upstream-failure",
2239 [DNSSEC_TOO_MANY_VALIDATIONS
] = "too-many-validations",
2241 DEFINE_STRING_TABLE_LOOKUP(dnssec_result
, DnssecResult
);
2243 static const char* const dnssec_verdict_table
[_DNSSEC_VERDICT_MAX
] = {
2244 [DNSSEC_SECURE
] = "secure",
2245 [DNSSEC_INSECURE
] = "insecure",
2246 [DNSSEC_BOGUS
] = "bogus",
2247 [DNSSEC_INDETERMINATE
] = "indeterminate",
2249 DEFINE_STRING_TABLE_LOOKUP(dnssec_verdict
, DnssecVerdict
);