/contrib/bind9/lib/dns/validator.c
https://bitbucket.org/freebsd/freebsd-head/ · C · 4369 lines · 3257 code · 378 blank · 734 comment · 1178 complexity · a9e623957e291c360bd8b11e4018b46b MD5 · raw file
Large files are truncated click here to view the full file
- /*
- * Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
- * Copyright (C) 2000-2003 Internet Software Consortium.
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
- * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
- * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
- * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
- * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
- * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
- * PERFORMANCE OF THIS SOFTWARE.
- */
- /* $Id$ */
- #include <config.h>
- #include <isc/base32.h>
- #include <isc/mem.h>
- #include <isc/print.h>
- #include <isc/sha2.h>
- #include <isc/string.h>
- #include <isc/task.h>
- #include <isc/util.h>
- #include <dns/db.h>
- #include <dns/dnssec.h>
- #include <dns/ds.h>
- #include <dns/events.h>
- #include <dns/keytable.h>
- #include <dns/keyvalues.h>
- #include <dns/log.h>
- #include <dns/message.h>
- #include <dns/ncache.h>
- #include <dns/nsec.h>
- #include <dns/nsec3.h>
- #include <dns/rdata.h>
- #include <dns/rdataset.h>
- #include <dns/rdatatype.h>
- #include <dns/resolver.h>
- #include <dns/result.h>
- #include <dns/validator.h>
- #include <dns/view.h>
- /*! \file
- * \brief
- * Basic processing sequences.
- *
- * \li When called with rdataset and sigrdataset:
- * validator_start -> validate -> proveunsecure -> startfinddlvsep ->
- * dlv_validator_start -> validator_start -> validate -> proveunsecure
- *
- * validator_start -> validate -> nsecvalidate (secure wildcard answer)
- *
- * \li When called with rdataset, sigrdataset and with DNS_VALIDATOR_DLV:
- * validator_start -> startfinddlvsep -> dlv_validator_start ->
- * validator_start -> validate -> proveunsecure
- *
- * \li When called with rdataset:
- * validator_start -> proveunsecure -> startfinddlvsep ->
- * dlv_validator_start -> validator_start -> proveunsecure
- *
- * \li When called with rdataset and with DNS_VALIDATOR_DLV:
- * validator_start -> startfinddlvsep -> dlv_validator_start ->
- * validator_start -> proveunsecure
- *
- * \li When called without a rdataset:
- * validator_start -> nsecvalidate -> proveunsecure -> startfinddlvsep ->
- * dlv_validator_start -> validator_start -> nsecvalidate -> proveunsecure
- *
- * Note: there isn't a case for DNS_VALIDATOR_DLV here as we want nsecvalidate()
- * to always validate the authority section even when it does not contain
- * signatures.
- *
- * validator_start: determines what type of validation to do.
- * validate: attempts to perform a positive validation.
- * proveunsecure: attempts to prove the answer comes from a unsecure zone.
- * nsecvalidate: attempts to prove a negative response.
- * startfinddlvsep: starts the DLV record lookup.
- * dlv_validator_start: resets state and restarts the lookup using the
- * DLV RRset found by startfinddlvsep.
- */
- #define VALIDATOR_MAGIC ISC_MAGIC('V', 'a', 'l', '?')
- #define VALID_VALIDATOR(v) ISC_MAGIC_VALID(v, VALIDATOR_MAGIC)
- #define VALATTR_SHUTDOWN 0x0001 /*%< Shutting down. */
- #define VALATTR_CANCELED 0x0002 /*%< Canceled. */
- #define VALATTR_TRIEDVERIFY 0x0004 /*%< We have found a key and
- * have attempted a verify. */
- #define VALATTR_INSECURITY 0x0010 /*%< Attempting proveunsecure. */
- #define VALATTR_DLVTRIED 0x0020 /*%< Looked for a DLV record. */
- /*!
- * NSEC proofs to be looked for.
- */
- #define VALATTR_NEEDNOQNAME 0x00000100
- #define VALATTR_NEEDNOWILDCARD 0x00000200
- #define VALATTR_NEEDNODATA 0x00000400
- /*!
- * NSEC proofs that have been found.
- */
- #define VALATTR_FOUNDNOQNAME 0x00001000
- #define VALATTR_FOUNDNOWILDCARD 0x00002000
- #define VALATTR_FOUNDNODATA 0x00004000
- #define VALATTR_FOUNDCLOSEST 0x00008000
- /*
- *
- */
- #define VALATTR_FOUNDOPTOUT 0x00010000
- #define VALATTR_FOUNDUNKNOWN 0x00020000
- #define NEEDNODATA(val) ((val->attributes & VALATTR_NEEDNODATA) != 0)
- #define NEEDNOQNAME(val) ((val->attributes & VALATTR_NEEDNOQNAME) != 0)
- #define NEEDNOWILDCARD(val) ((val->attributes & VALATTR_NEEDNOWILDCARD) != 0)
- #define DLVTRIED(val) ((val->attributes & VALATTR_DLVTRIED) != 0)
- #define FOUNDNODATA(val) ((val->attributes & VALATTR_FOUNDNODATA) != 0)
- #define FOUNDNOQNAME(val) ((val->attributes & VALATTR_FOUNDNOQNAME) != 0)
- #define FOUNDNOWILDCARD(val) ((val->attributes & VALATTR_FOUNDNOWILDCARD) != 0)
- #define FOUNDCLOSEST(val) ((val->attributes & VALATTR_FOUNDCLOSEST) != 0)
- #define FOUNDOPTOUT(val) ((val->attributes & VALATTR_FOUNDOPTOUT) != 0)
- #define SHUTDOWN(v) (((v)->attributes & VALATTR_SHUTDOWN) != 0)
- #define CANCELED(v) (((v)->attributes & VALATTR_CANCELED) != 0)
- #define NEGATIVE(r) (((r)->attributes & DNS_RDATASETATTR_NEGATIVE) != 0)
- static void
- destroy(dns_validator_t *val);
- static isc_result_t
- get_dst_key(dns_validator_t *val, dns_rdata_rrsig_t *siginfo,
- dns_rdataset_t *rdataset);
- static isc_result_t
- validate(dns_validator_t *val, isc_boolean_t resume);
- static isc_result_t
- validatezonekey(dns_validator_t *val);
- static isc_result_t
- nsecvalidate(dns_validator_t *val, isc_boolean_t resume);
- static isc_result_t
- proveunsecure(dns_validator_t *val, isc_boolean_t have_ds,
- isc_boolean_t resume);
- static void
- validator_logv(dns_validator_t *val, isc_logcategory_t *category,
- isc_logmodule_t *module, int level, const char *fmt, va_list ap)
- ISC_FORMAT_PRINTF(5, 0);
- static void
- validator_log(dns_validator_t *val, int level, const char *fmt, ...)
- ISC_FORMAT_PRINTF(3, 4);
- static void
- validator_logcreate(dns_validator_t *val,
- dns_name_t *name, dns_rdatatype_t type,
- const char *caller, const char *operation);
- static isc_result_t
- dlv_validatezonekey(dns_validator_t *val);
- static void
- dlv_validator_start(dns_validator_t *val);
- static isc_result_t
- finddlvsep(dns_validator_t *val, isc_boolean_t resume);
- static isc_result_t
- startfinddlvsep(dns_validator_t *val, dns_name_t *unsecure);
- /*%
- * Mark the RRsets as a answer.
- */
- static inline void
- markanswer(dns_validator_t *val, const char *where) {
- validator_log(val, ISC_LOG_DEBUG(3), "marking as answer (%s)", where);
- if (val->event->rdataset != NULL)
- dns_rdataset_settrust(val->event->rdataset, dns_trust_answer);
- if (val->event->sigrdataset != NULL)
- dns_rdataset_settrust(val->event->sigrdataset,
- dns_trust_answer);
- }
- static inline void
- marksecure(dns_validatorevent_t *event) {
- dns_rdataset_settrust(event->rdataset, dns_trust_secure);
- if (event->sigrdataset != NULL)
- dns_rdataset_settrust(event->sigrdataset, dns_trust_secure);
- }
- static void
- validator_done(dns_validator_t *val, isc_result_t result) {
- isc_task_t *task;
- if (val->event == NULL)
- return;
- /*
- * Caller must be holding the lock.
- */
- val->event->result = result;
- task = val->event->ev_sender;
- val->event->ev_sender = val;
- val->event->ev_type = DNS_EVENT_VALIDATORDONE;
- val->event->ev_action = val->action;
- val->event->ev_arg = val->arg;
- isc_task_sendanddetach(&task, (isc_event_t **)&val->event);
- }
- static inline isc_boolean_t
- exit_check(dns_validator_t *val) {
- /*
- * Caller must be holding the lock.
- */
- if (!SHUTDOWN(val))
- return (ISC_FALSE);
- INSIST(val->event == NULL);
- if (val->fetch != NULL || val->subvalidator != NULL)
- return (ISC_FALSE);
- return (ISC_TRUE);
- }
- /*
- * Check that we have atleast one supported algorithm in the DLV RRset.
- */
- static inline isc_boolean_t
- dlv_algorithm_supported(dns_validator_t *val) {
- dns_rdata_t rdata = DNS_RDATA_INIT;
- dns_rdata_dlv_t dlv;
- isc_result_t result;
- for (result = dns_rdataset_first(&val->dlv);
- result == ISC_R_SUCCESS;
- result = dns_rdataset_next(&val->dlv)) {
- dns_rdata_reset(&rdata);
- dns_rdataset_current(&val->dlv, &rdata);
- result = dns_rdata_tostruct(&rdata, &dlv, NULL);
- RUNTIME_CHECK(result == ISC_R_SUCCESS);
- if (!dns_resolver_algorithm_supported(val->view->resolver,
- val->event->name,
- dlv.algorithm))
- continue;
- #ifdef HAVE_OPENSSL_GOST
- if (dlv.digest_type != DNS_DSDIGEST_SHA256 &&
- dlv.digest_type != DNS_DSDIGEST_SHA1 &&
- dlv.digest_type != DNS_DSDIGEST_GOST)
- continue;
- #else
- if (dlv.digest_type != DNS_DSDIGEST_SHA256 &&
- dlv.digest_type != DNS_DSDIGEST_SHA1)
- continue;
- #endif
- return (ISC_TRUE);
- }
- return (ISC_FALSE);
- }
- /*%
- * Look in the NSEC record returned from a DS query to see if there is
- * a NS RRset at this name. If it is found we are at a delegation point.
- */
- static isc_boolean_t
- isdelegation(dns_name_t *name, dns_rdataset_t *rdataset,
- isc_result_t dbresult)
- {
- dns_fixedname_t fixed;
- dns_label_t hashlabel;
- dns_name_t nsec3name;
- dns_rdata_nsec3_t nsec3;
- dns_rdata_t rdata = DNS_RDATA_INIT;
- dns_rdataset_t set;
- int order;
- int scope;
- isc_boolean_t found;
- isc_buffer_t buffer;
- isc_result_t result;
- unsigned char hash[NSEC3_MAX_HASH_LENGTH];
- unsigned char owner[NSEC3_MAX_HASH_LENGTH];
- unsigned int length;
- REQUIRE(dbresult == DNS_R_NXRRSET || dbresult == DNS_R_NCACHENXRRSET);
- dns_rdataset_init(&set);
- if (dbresult == DNS_R_NXRRSET)
- dns_rdataset_clone(rdataset, &set);
- else {
- result = dns_ncache_getrdataset(rdataset, name,
- dns_rdatatype_nsec, &set);
- if (result == ISC_R_NOTFOUND)
- goto trynsec3;
- if (result != ISC_R_SUCCESS)
- return (ISC_FALSE);
- }
- INSIST(set.type == dns_rdatatype_nsec);
- found = ISC_FALSE;
- result = dns_rdataset_first(&set);
- if (result == ISC_R_SUCCESS) {
- dns_rdataset_current(&set, &rdata);
- found = dns_nsec_typepresent(&rdata, dns_rdatatype_ns);
- dns_rdata_reset(&rdata);
- }
- dns_rdataset_disassociate(&set);
- return (found);
- trynsec3:
- /*
- * Iterate over the ncache entry.
- */
- found = ISC_FALSE;
- dns_name_init(&nsec3name, NULL);
- dns_fixedname_init(&fixed);
- dns_name_downcase(name, dns_fixedname_name(&fixed), NULL);
- name = dns_fixedname_name(&fixed);
- for (result = dns_rdataset_first(rdataset);
- result == ISC_R_SUCCESS;
- result = dns_rdataset_next(rdataset))
- {
- dns_ncache_current(rdataset, &nsec3name, &set);
- if (set.type != dns_rdatatype_nsec3) {
- dns_rdataset_disassociate(&set);
- continue;
- }
- dns_name_getlabel(&nsec3name, 0, &hashlabel);
- isc_region_consume(&hashlabel, 1);
- isc_buffer_init(&buffer, owner, sizeof(owner));
- result = isc_base32hex_decoderegion(&hashlabel, &buffer);
- if (result != ISC_R_SUCCESS) {
- dns_rdataset_disassociate(&set);
- continue;
- }
- for (result = dns_rdataset_first(&set);
- result == ISC_R_SUCCESS;
- result = dns_rdataset_next(&set))
- {
- dns_rdata_reset(&rdata);
- dns_rdataset_current(&set, &rdata);
- (void)dns_rdata_tostruct(&rdata, &nsec3, NULL);
- if (nsec3.hash != 1)
- continue;
- length = isc_iterated_hash(hash, nsec3.hash,
- nsec3.iterations, nsec3.salt,
- nsec3.salt_length,
- name->ndata, name->length);
- if (length != isc_buffer_usedlength(&buffer))
- continue;
- order = memcmp(hash, owner, length);
- if (order == 0) {
- found = dns_nsec3_typepresent(&rdata,
- dns_rdatatype_ns);
- dns_rdataset_disassociate(&set);
- return (found);
- }
- if ((nsec3.flags & DNS_NSEC3FLAG_OPTOUT) == 0)
- continue;
- /*
- * Does this optout span cover the name?
- */
- scope = memcmp(owner, nsec3.next, nsec3.next_length);
- if ((scope < 0 && order > 0 &&
- memcmp(hash, nsec3.next, length) < 0) ||
- (scope >= 0 && (order > 0 ||
- memcmp(hash, nsec3.next, length) < 0)))
- {
- dns_rdataset_disassociate(&set);
- return (ISC_TRUE);
- }
- }
- dns_rdataset_disassociate(&set);
- }
- return (found);
- }
- /*%
- * We have been asked to look for a key.
- * If found resume the validation process.
- * If not found fail the validation process.
- */
- static void
- fetch_callback_validator(isc_task_t *task, isc_event_t *event) {
- dns_fetchevent_t *devent;
- dns_validator_t *val;
- dns_rdataset_t *rdataset;
- isc_boolean_t want_destroy;
- isc_result_t result;
- isc_result_t eresult;
- isc_result_t saved_result;
- UNUSED(task);
- INSIST(event->ev_type == DNS_EVENT_FETCHDONE);
- devent = (dns_fetchevent_t *)event;
- val = devent->ev_arg;
- rdataset = &val->frdataset;
- eresult = devent->result;
- /* Free resources which are not of interest. */
- if (devent->node != NULL)
- dns_db_detachnode(devent->db, &devent->node);
- if (devent->db != NULL)
- dns_db_detach(&devent->db);
- if (dns_rdataset_isassociated(&val->fsigrdataset))
- dns_rdataset_disassociate(&val->fsigrdataset);
- isc_event_free(&event);
- dns_resolver_destroyfetch(&val->fetch);
- INSIST(val->event != NULL);
- validator_log(val, ISC_LOG_DEBUG(3), "in fetch_callback_validator");
- LOCK(&val->lock);
- if (CANCELED(val)) {
- validator_done(val, ISC_R_CANCELED);
- } else if (eresult == ISC_R_SUCCESS) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "keyset with trust %s",
- dns_trust_totext(rdataset->trust));
- /*
- * Only extract the dst key if the keyset is secure.
- */
- if (rdataset->trust >= dns_trust_secure) {
- result = get_dst_key(val, val->siginfo, rdataset);
- if (result == ISC_R_SUCCESS)
- val->keyset = &val->frdataset;
- }
- result = validate(val, ISC_TRUE);
- if (result == DNS_R_NOVALIDSIG &&
- (val->attributes & VALATTR_TRIEDVERIFY) == 0)
- {
- saved_result = result;
- validator_log(val, ISC_LOG_DEBUG(3),
- "falling back to insecurity proof");
- val->attributes |= VALATTR_INSECURITY;
- result = proveunsecure(val, ISC_FALSE, ISC_FALSE);
- if (result == DNS_R_NOTINSECURE)
- result = saved_result;
- }
- if (result != DNS_R_WAIT)
- validator_done(val, result);
- } else {
- validator_log(val, ISC_LOG_DEBUG(3),
- "fetch_callback_validator: got %s",
- isc_result_totext(eresult));
- if (eresult == ISC_R_CANCELED)
- validator_done(val, eresult);
- else
- validator_done(val, DNS_R_BROKENCHAIN);
- }
- want_destroy = exit_check(val);
- UNLOCK(&val->lock);
- if (want_destroy)
- destroy(val);
- }
- /*%
- * We were asked to look for a DS record as part of following a key chain
- * upwards. If found resume the validation process. If not found fail the
- * validation process.
- */
- static void
- dsfetched(isc_task_t *task, isc_event_t *event) {
- dns_fetchevent_t *devent;
- dns_validator_t *val;
- dns_rdataset_t *rdataset;
- isc_boolean_t want_destroy;
- isc_result_t result;
- isc_result_t eresult;
- UNUSED(task);
- INSIST(event->ev_type == DNS_EVENT_FETCHDONE);
- devent = (dns_fetchevent_t *)event;
- val = devent->ev_arg;
- rdataset = &val->frdataset;
- eresult = devent->result;
- /* Free resources which are not of interest. */
- if (devent->node != NULL)
- dns_db_detachnode(devent->db, &devent->node);
- if (devent->db != NULL)
- dns_db_detach(&devent->db);
- if (dns_rdataset_isassociated(&val->fsigrdataset))
- dns_rdataset_disassociate(&val->fsigrdataset);
- isc_event_free(&event);
- dns_resolver_destroyfetch(&val->fetch);
- INSIST(val->event != NULL);
- validator_log(val, ISC_LOG_DEBUG(3), "in dsfetched");
- LOCK(&val->lock);
- if (CANCELED(val)) {
- validator_done(val, ISC_R_CANCELED);
- } else if (eresult == ISC_R_SUCCESS) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "dsset with trust %s",
- dns_trust_totext(rdataset->trust));
- val->dsset = &val->frdataset;
- result = validatezonekey(val);
- if (result != DNS_R_WAIT)
- validator_done(val, result);
- } else if (eresult == DNS_R_CNAME ||
- eresult == DNS_R_NXRRSET ||
- eresult == DNS_R_NCACHENXRRSET ||
- eresult == DNS_R_SERVFAIL) /* RFC 1034 parent? */
- {
- validator_log(val, ISC_LOG_DEBUG(3),
- "falling back to insecurity proof (%s)",
- dns_result_totext(eresult));
- val->attributes |= VALATTR_INSECURITY;
- result = proveunsecure(val, ISC_FALSE, ISC_FALSE);
- if (result != DNS_R_WAIT)
- validator_done(val, result);
- } else {
- validator_log(val, ISC_LOG_DEBUG(3),
- "dsfetched: got %s",
- isc_result_totext(eresult));
- if (eresult == ISC_R_CANCELED)
- validator_done(val, eresult);
- else
- validator_done(val, DNS_R_BROKENCHAIN);
- }
- want_destroy = exit_check(val);
- UNLOCK(&val->lock);
- if (want_destroy)
- destroy(val);
- }
- /*%
- * We were asked to look for the DS record as part of proving that a
- * name is unsecure.
- *
- * If the DS record doesn't exist and the query name corresponds to
- * a delegation point we are transitioning from a secure zone to a
- * unsecure zone.
- *
- * If the DS record exists it will be secure. We can continue looking
- * for the break point in the chain of trust.
- */
- static void
- dsfetched2(isc_task_t *task, isc_event_t *event) {
- dns_fetchevent_t *devent;
- dns_validator_t *val;
- dns_name_t *tname;
- isc_boolean_t want_destroy;
- isc_result_t result;
- isc_result_t eresult;
- UNUSED(task);
- INSIST(event->ev_type == DNS_EVENT_FETCHDONE);
- devent = (dns_fetchevent_t *)event;
- val = devent->ev_arg;
- eresult = devent->result;
- /* Free resources which are not of interest. */
- if (devent->node != NULL)
- dns_db_detachnode(devent->db, &devent->node);
- if (devent->db != NULL)
- dns_db_detach(&devent->db);
- if (dns_rdataset_isassociated(&val->fsigrdataset))
- dns_rdataset_disassociate(&val->fsigrdataset);
- dns_resolver_destroyfetch(&val->fetch);
- INSIST(val->event != NULL);
- validator_log(val, ISC_LOG_DEBUG(3), "in dsfetched2: %s",
- dns_result_totext(eresult));
- LOCK(&val->lock);
- if (CANCELED(val)) {
- validator_done(val, ISC_R_CANCELED);
- } else if (eresult == DNS_R_CNAME ||
- eresult == DNS_R_NXRRSET ||
- eresult == DNS_R_NCACHENXRRSET)
- {
- /*
- * There is no DS. If this is a delegation, we're done.
- */
- tname = dns_fixedname_name(&devent->foundname);
- if (eresult != DNS_R_CNAME &&
- isdelegation(tname, &val->frdataset, eresult)) {
- if (val->mustbesecure) {
- validator_log(val, ISC_LOG_WARNING,
- "must be secure failure, no DS"
- " and this is a delegation");
- validator_done(val, DNS_R_MUSTBESECURE);
- } else if (val->view->dlv == NULL || DLVTRIED(val)) {
- markanswer(val, "dsfetched2");
- validator_done(val, ISC_R_SUCCESS);
- } else {
- result = startfinddlvsep(val, tname);
- if (result != DNS_R_WAIT)
- validator_done(val, result);
- }
- } else {
- result = proveunsecure(val, ISC_FALSE, ISC_TRUE);
- if (result != DNS_R_WAIT)
- validator_done(val, result);
- }
- } else if (eresult == ISC_R_SUCCESS ||
- eresult == DNS_R_NXDOMAIN ||
- eresult == DNS_R_NCACHENXDOMAIN)
- {
- /*
- * There is a DS which may or may not be a zone cut.
- * In either case we are still in a secure zone resume
- * validation.
- */
- result = proveunsecure(val, ISC_TF(eresult == ISC_R_SUCCESS),
- ISC_TRUE);
- if (result != DNS_R_WAIT)
- validator_done(val, result);
- } else {
- if (eresult == ISC_R_CANCELED)
- validator_done(val, eresult);
- else
- validator_done(val, DNS_R_NOVALIDDS);
- }
- isc_event_free(&event);
- want_destroy = exit_check(val);
- UNLOCK(&val->lock);
- if (want_destroy)
- destroy(val);
- }
- /*%
- * Callback from when a DNSKEY RRset has been validated.
- *
- * Resumes the stalled validation process.
- */
- static void
- keyvalidated(isc_task_t *task, isc_event_t *event) {
- dns_validatorevent_t *devent;
- dns_validator_t *val;
- isc_boolean_t want_destroy;
- isc_result_t result;
- isc_result_t eresult;
- isc_result_t saved_result;
- UNUSED(task);
- INSIST(event->ev_type == DNS_EVENT_VALIDATORDONE);
- devent = (dns_validatorevent_t *)event;
- val = devent->ev_arg;
- eresult = devent->result;
- isc_event_free(&event);
- dns_validator_destroy(&val->subvalidator);
- INSIST(val->event != NULL);
- validator_log(val, ISC_LOG_DEBUG(3), "in keyvalidated");
- LOCK(&val->lock);
- if (CANCELED(val)) {
- validator_done(val, ISC_R_CANCELED);
- } else if (eresult == ISC_R_SUCCESS) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "keyset with trust %s",
- dns_trust_totext(val->frdataset.trust));
- /*
- * Only extract the dst key if the keyset is secure.
- */
- if (val->frdataset.trust >= dns_trust_secure)
- (void) get_dst_key(val, val->siginfo, &val->frdataset);
- result = validate(val, ISC_TRUE);
- if (result == DNS_R_NOVALIDSIG &&
- (val->attributes & VALATTR_TRIEDVERIFY) == 0)
- {
- saved_result = result;
- validator_log(val, ISC_LOG_DEBUG(3),
- "falling back to insecurity proof");
- val->attributes |= VALATTR_INSECURITY;
- result = proveunsecure(val, ISC_FALSE, ISC_FALSE);
- if (result == DNS_R_NOTINSECURE)
- result = saved_result;
- }
- if (result != DNS_R_WAIT)
- validator_done(val, result);
- } else {
- if (eresult != DNS_R_BROKENCHAIN) {
- if (dns_rdataset_isassociated(&val->frdataset))
- dns_rdataset_expire(&val->frdataset);
- if (dns_rdataset_isassociated(&val->fsigrdataset))
- dns_rdataset_expire(&val->fsigrdataset);
- }
- validator_log(val, ISC_LOG_DEBUG(3),
- "keyvalidated: got %s",
- isc_result_totext(eresult));
- validator_done(val, DNS_R_BROKENCHAIN);
- }
- want_destroy = exit_check(val);
- UNLOCK(&val->lock);
- if (want_destroy)
- destroy(val);
- }
- /*%
- * Callback when the DS record has been validated.
- *
- * Resumes validation of the zone key or the unsecure zone proof.
- */
- static void
- dsvalidated(isc_task_t *task, isc_event_t *event) {
- dns_validatorevent_t *devent;
- dns_validator_t *val;
- isc_boolean_t want_destroy;
- isc_result_t result;
- isc_result_t eresult;
- UNUSED(task);
- INSIST(event->ev_type == DNS_EVENT_VALIDATORDONE);
- devent = (dns_validatorevent_t *)event;
- val = devent->ev_arg;
- eresult = devent->result;
- isc_event_free(&event);
- dns_validator_destroy(&val->subvalidator);
- INSIST(val->event != NULL);
- validator_log(val, ISC_LOG_DEBUG(3), "in dsvalidated");
- LOCK(&val->lock);
- if (CANCELED(val)) {
- validator_done(val, ISC_R_CANCELED);
- } else if (eresult == ISC_R_SUCCESS) {
- isc_boolean_t have_dsset;
- dns_name_t *name;
- validator_log(val, ISC_LOG_DEBUG(3),
- "%s with trust %s",
- val->frdataset.type == dns_rdatatype_ds ?
- "dsset" : "ds non-existance",
- dns_trust_totext(val->frdataset.trust));
- have_dsset = ISC_TF(val->frdataset.type == dns_rdatatype_ds);
- name = dns_fixedname_name(&val->fname);
- if ((val->attributes & VALATTR_INSECURITY) != 0 &&
- val->frdataset.covers == dns_rdatatype_ds &&
- NEGATIVE(&val->frdataset) &&
- isdelegation(name, &val->frdataset, DNS_R_NCACHENXRRSET)) {
- if (val->mustbesecure) {
- validator_log(val, ISC_LOG_WARNING,
- "must be secure failure, no DS "
- "and this is a delegation");
- result = DNS_R_MUSTBESECURE;
- } else if (val->view->dlv == NULL || DLVTRIED(val)) {
- markanswer(val, "dsvalidated");
- result = ISC_R_SUCCESS;;
- } else
- result = startfinddlvsep(val, name);
- } else if ((val->attributes & VALATTR_INSECURITY) != 0) {
- result = proveunsecure(val, have_dsset, ISC_TRUE);
- } else
- result = validatezonekey(val);
- if (result != DNS_R_WAIT)
- validator_done(val, result);
- } else {
- if (eresult != DNS_R_BROKENCHAIN) {
- if (dns_rdataset_isassociated(&val->frdataset))
- dns_rdataset_expire(&val->frdataset);
- if (dns_rdataset_isassociated(&val->fsigrdataset))
- dns_rdataset_expire(&val->fsigrdataset);
- }
- validator_log(val, ISC_LOG_DEBUG(3),
- "dsvalidated: got %s",
- isc_result_totext(eresult));
- validator_done(val, DNS_R_BROKENCHAIN);
- }
- want_destroy = exit_check(val);
- UNLOCK(&val->lock);
- if (want_destroy)
- destroy(val);
- }
- /*%
- * Callback when the CNAME record has been validated.
- *
- * Resumes validation of the unsecure zone proof.
- */
- static void
- cnamevalidated(isc_task_t *task, isc_event_t *event) {
- dns_validatorevent_t *devent;
- dns_validator_t *val;
- isc_boolean_t want_destroy;
- isc_result_t result;
- isc_result_t eresult;
- UNUSED(task);
- INSIST(event->ev_type == DNS_EVENT_VALIDATORDONE);
- devent = (dns_validatorevent_t *)event;
- val = devent->ev_arg;
- eresult = devent->result;
- isc_event_free(&event);
- dns_validator_destroy(&val->subvalidator);
- INSIST(val->event != NULL);
- INSIST((val->attributes & VALATTR_INSECURITY) != 0);
- validator_log(val, ISC_LOG_DEBUG(3), "in cnamevalidated");
- LOCK(&val->lock);
- if (CANCELED(val)) {
- validator_done(val, ISC_R_CANCELED);
- } else if (eresult == ISC_R_SUCCESS) {
- validator_log(val, ISC_LOG_DEBUG(3), "cname with trust %s",
- dns_trust_totext(val->frdataset.trust));
- result = proveunsecure(val, ISC_FALSE, ISC_TRUE);
- if (result != DNS_R_WAIT)
- validator_done(val, result);
- } else {
- if (eresult != DNS_R_BROKENCHAIN) {
- if (dns_rdataset_isassociated(&val->frdataset))
- dns_rdataset_expire(&val->frdataset);
- if (dns_rdataset_isassociated(&val->fsigrdataset))
- dns_rdataset_expire(&val->fsigrdataset);
- }
- validator_log(val, ISC_LOG_DEBUG(3),
- "cnamevalidated: got %s",
- isc_result_totext(eresult));
- validator_done(val, DNS_R_BROKENCHAIN);
- }
- want_destroy = exit_check(val);
- UNLOCK(&val->lock);
- if (want_destroy)
- destroy(val);
- }
- /*%
- * Return ISC_R_SUCCESS if we can determine that the name doesn't exist
- * or we can determine whether there is data or not at the name.
- * If the name does not exist return the wildcard name.
- *
- * Return ISC_R_IGNORE when the NSEC is not the appropriate one.
- */
- static isc_result_t
- nsecnoexistnodata(dns_validator_t *val, dns_name_t *name, dns_name_t *nsecname,
- dns_rdataset_t *nsecset, isc_boolean_t *exists,
- isc_boolean_t *data, dns_name_t *wild)
- {
- int order;
- dns_rdata_t rdata = DNS_RDATA_INIT;
- isc_result_t result;
- dns_namereln_t relation;
- unsigned int olabels, nlabels, labels;
- dns_rdata_nsec_t nsec;
- isc_boolean_t atparent;
- isc_boolean_t ns;
- isc_boolean_t soa;
- REQUIRE(exists != NULL);
- REQUIRE(data != NULL);
- REQUIRE(nsecset != NULL &&
- nsecset->type == dns_rdatatype_nsec);
- result = dns_rdataset_first(nsecset);
- if (result != ISC_R_SUCCESS) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "failure processing NSEC set");
- return (result);
- }
- dns_rdataset_current(nsecset, &rdata);
- validator_log(val, ISC_LOG_DEBUG(3), "looking for relevant nsec");
- relation = dns_name_fullcompare(name, nsecname, &order, &olabels);
- if (order < 0) {
- /*
- * The name is not within the NSEC range.
- */
- validator_log(val, ISC_LOG_DEBUG(3),
- "NSEC does not cover name, before NSEC");
- return (ISC_R_IGNORE);
- }
- if (order == 0) {
- /*
- * The names are the same. If we are validating "."
- * then atparent should not be set as there is no parent.
- */
- atparent = (olabels != 1) &&
- dns_rdatatype_atparent(val->event->type);
- ns = dns_nsec_typepresent(&rdata, dns_rdatatype_ns);
- soa = dns_nsec_typepresent(&rdata, dns_rdatatype_soa);
- if (ns && !soa) {
- if (!atparent) {
- /*
- * This NSEC record is from somewhere higher in
- * the DNS, and at the parent of a delegation.
- * It can not be legitimately used here.
- */
- validator_log(val, ISC_LOG_DEBUG(3),
- "ignoring parent nsec");
- return (ISC_R_IGNORE);
- }
- } else if (atparent && ns && soa) {
- /*
- * This NSEC record is from the child.
- * It can not be legitimately used here.
- */
- validator_log(val, ISC_LOG_DEBUG(3),
- "ignoring child nsec");
- return (ISC_R_IGNORE);
- }
- if (val->event->type == dns_rdatatype_cname ||
- val->event->type == dns_rdatatype_nxt ||
- val->event->type == dns_rdatatype_nsec ||
- val->event->type == dns_rdatatype_key ||
- !dns_nsec_typepresent(&rdata, dns_rdatatype_cname)) {
- *exists = ISC_TRUE;
- *data = dns_nsec_typepresent(&rdata, val->event->type);
- validator_log(val, ISC_LOG_DEBUG(3),
- "nsec proves name exists (owner) data=%d",
- *data);
- return (ISC_R_SUCCESS);
- }
- validator_log(val, ISC_LOG_DEBUG(3), "NSEC proves CNAME exists");
- return (ISC_R_IGNORE);
- }
- if (relation == dns_namereln_subdomain &&
- dns_nsec_typepresent(&rdata, dns_rdatatype_ns) &&
- !dns_nsec_typepresent(&rdata, dns_rdatatype_soa))
- {
- /*
- * This NSEC record is from somewhere higher in
- * the DNS, and at the parent of a delegation.
- * It can not be legitimately used here.
- */
- validator_log(val, ISC_LOG_DEBUG(3), "ignoring parent nsec");
- return (ISC_R_IGNORE);
- }
- result = dns_rdata_tostruct(&rdata, &nsec, NULL);
- if (result != ISC_R_SUCCESS)
- return (result);
- relation = dns_name_fullcompare(&nsec.next, name, &order, &nlabels);
- if (order == 0) {
- dns_rdata_freestruct(&nsec);
- validator_log(val, ISC_LOG_DEBUG(3),
- "ignoring nsec matches next name");
- return (ISC_R_IGNORE);
- }
- if (order < 0 && !dns_name_issubdomain(nsecname, &nsec.next)) {
- /*
- * The name is not within the NSEC range.
- */
- dns_rdata_freestruct(&nsec);
- validator_log(val, ISC_LOG_DEBUG(3),
- "ignoring nsec because name is past end of range");
- return (ISC_R_IGNORE);
- }
- if (order > 0 && relation == dns_namereln_subdomain) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "nsec proves name exist (empty)");
- dns_rdata_freestruct(&nsec);
- *exists = ISC_TRUE;
- *data = ISC_FALSE;
- return (ISC_R_SUCCESS);
- }
- if (wild != NULL) {
- dns_name_t common;
- dns_name_init(&common, NULL);
- if (olabels > nlabels) {
- labels = dns_name_countlabels(nsecname);
- dns_name_getlabelsequence(nsecname, labels - olabels,
- olabels, &common);
- } else {
- labels = dns_name_countlabels(&nsec.next);
- dns_name_getlabelsequence(&nsec.next, labels - nlabels,
- nlabels, &common);
- }
- result = dns_name_concatenate(dns_wildcardname, &common,
- wild, NULL);
- if (result != ISC_R_SUCCESS) {
- dns_rdata_freestruct(&nsec);
- validator_log(val, ISC_LOG_DEBUG(3),
- "failure generating wildcard name");
- return (result);
- }
- }
- dns_rdata_freestruct(&nsec);
- validator_log(val, ISC_LOG_DEBUG(3), "nsec range ok");
- *exists = ISC_FALSE;
- return (ISC_R_SUCCESS);
- }
- static isc_result_t
- nsec3noexistnodata(dns_validator_t *val, dns_name_t* name,
- dns_name_t *nsec3name, dns_rdataset_t *nsec3set,
- dns_name_t *zonename, isc_boolean_t *exists,
- isc_boolean_t *data, isc_boolean_t *optout,
- isc_boolean_t *unknown, isc_boolean_t *setclosest,
- isc_boolean_t *setnearest, dns_name_t *closest,
- dns_name_t *nearest)
- {
- char namebuf[DNS_NAME_FORMATSIZE];
- dns_fixedname_t fzone;
- dns_fixedname_t qfixed;
- dns_label_t hashlabel;
- dns_name_t *qname;
- dns_name_t *zone;
- dns_rdata_nsec3_t nsec3;
- dns_rdata_t rdata = DNS_RDATA_INIT;
- int order;
- int scope;
- isc_boolean_t atparent;
- isc_boolean_t first;
- isc_boolean_t ns;
- isc_boolean_t soa;
- isc_buffer_t buffer;
- isc_result_t answer = ISC_R_IGNORE;
- isc_result_t result;
- unsigned char hash[NSEC3_MAX_HASH_LENGTH];
- unsigned char owner[NSEC3_MAX_HASH_LENGTH];
- unsigned int length;
- unsigned int qlabels;
- unsigned int zlabels;
- REQUIRE((exists == NULL && data == NULL) ||
- (exists != NULL && data != NULL));
- REQUIRE(nsec3set != NULL && nsec3set->type == dns_rdatatype_nsec3);
- REQUIRE((setclosest == NULL && closest == NULL) ||
- (setclosest != NULL && closest != NULL));
- REQUIRE((setnearest == NULL && nearest == NULL) ||
- (setnearest != NULL && nearest != NULL));
- result = dns_rdataset_first(nsec3set);
- if (result != ISC_R_SUCCESS) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "failure processing NSEC3 set");
- return (result);
- }
- dns_rdataset_current(nsec3set, &rdata);
- result = dns_rdata_tostruct(&rdata, &nsec3, NULL);
- if (result != ISC_R_SUCCESS)
- return (result);
- validator_log(val, ISC_LOG_DEBUG(3), "looking for relevant NSEC3");
- dns_fixedname_init(&fzone);
- zone = dns_fixedname_name(&fzone);
- zlabels = dns_name_countlabels(nsec3name);
- /*
- * NSEC3 records must have two or more labels to be valid.
- */
- if (zlabels < 2)
- return (ISC_R_IGNORE);
- /*
- * Strip off the NSEC3 hash to get the zone.
- */
- zlabels--;
- dns_name_split(nsec3name, zlabels, NULL, zone);
- /*
- * If not below the zone name we can ignore this record.
- */
- if (!dns_name_issubdomain(name, zone))
- return (ISC_R_IGNORE);
- /*
- * Is this zone the same or deeper than the current zone?
- */
- if (dns_name_countlabels(zonename) == 0 ||
- dns_name_issubdomain(zone, zonename))
- dns_name_copy(zone, zonename, NULL);
- if (!dns_name_equal(zone, zonename))
- return (ISC_R_IGNORE);
- /*
- * Are we only looking for the most enclosing zone?
- */
- if (exists == NULL || data == NULL)
- return (ISC_R_SUCCESS);
- /*
- * Only set unknown once we are sure that this NSEC3 is from
- * the deepest covering zone.
- */
- if (!dns_nsec3_supportedhash(nsec3.hash)) {
- if (unknown != NULL)
- *unknown = ISC_TRUE;
- return (ISC_R_IGNORE);
- }
- /*
- * Recover the hash from the first label.
- */
- dns_name_getlabel(nsec3name, 0, &hashlabel);
- isc_region_consume(&hashlabel, 1);
- isc_buffer_init(&buffer, owner, sizeof(owner));
- result = isc_base32hex_decoderegion(&hashlabel, &buffer);
- if (result != ISC_R_SUCCESS)
- return (result);
- /*
- * The hash lengths should match. If not ignore the record.
- */
- if (isc_buffer_usedlength(&buffer) != nsec3.next_length)
- return (ISC_R_IGNORE);
- /*
- * Work out what this NSEC3 covers.
- * Inside (<0) or outside (>=0).
- */
- scope = memcmp(owner, nsec3.next, nsec3.next_length);
- /*
- * Prepare to compute all the hashes.
- */
- dns_fixedname_init(&qfixed);
- qname = dns_fixedname_name(&qfixed);
- dns_name_downcase(name, qname, NULL);
- qlabels = dns_name_countlabels(qname);
- first = ISC_TRUE;
- while (qlabels >= zlabels) {
- length = isc_iterated_hash(hash, nsec3.hash, nsec3.iterations,
- nsec3.salt, nsec3.salt_length,
- qname->ndata, qname->length);
- /*
- * The computed hash length should match.
- */
- if (length != nsec3.next_length) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "ignoring NSEC bad length %u vs %u",
- length, nsec3.next_length);
- return (ISC_R_IGNORE);
- }
- order = memcmp(hash, owner, length);
- if (first && order == 0) {
- /*
- * The hashes are the same.
- */
- atparent = dns_rdatatype_atparent(val->event->type);
- ns = dns_nsec3_typepresent(&rdata, dns_rdatatype_ns);
- soa = dns_nsec3_typepresent(&rdata, dns_rdatatype_soa);
- if (ns && !soa) {
- if (!atparent) {
- /*
- * This NSEC3 record is from somewhere
- * higher in the DNS, and at the
- * parent of a delegation. It can not
- * be legitimately used here.
- */
- validator_log(val, ISC_LOG_DEBUG(3),
- "ignoring parent NSEC3");
- return (ISC_R_IGNORE);
- }
- } else if (atparent && ns && soa) {
- /*
- * This NSEC3 record is from the child.
- * It can not be legitimately used here.
- */
- validator_log(val, ISC_LOG_DEBUG(3),
- "ignoring child NSEC3");
- return (ISC_R_IGNORE);
- }
- if (val->event->type == dns_rdatatype_cname ||
- val->event->type == dns_rdatatype_nxt ||
- val->event->type == dns_rdatatype_nsec ||
- val->event->type == dns_rdatatype_key ||
- !dns_nsec3_typepresent(&rdata, dns_rdatatype_cname)) {
- *exists = ISC_TRUE;
- *data = dns_nsec3_typepresent(&rdata,
- val->event->type);
- validator_log(val, ISC_LOG_DEBUG(3),
- "NSEC3 proves name exists (owner) "
- "data=%d", *data);
- return (ISC_R_SUCCESS);
- }
- validator_log(val, ISC_LOG_DEBUG(3),
- "NSEC3 proves CNAME exists");
- return (ISC_R_IGNORE);
- }
- if (order == 0 &&
- dns_nsec3_typepresent(&rdata, dns_rdatatype_ns) &&
- !dns_nsec3_typepresent(&rdata, dns_rdatatype_soa))
- {
- /*
- * This NSEC3 record is from somewhere higher in
- * the DNS, and at the parent of a delegation.
- * It can not be legitimately used here.
- */
- validator_log(val, ISC_LOG_DEBUG(3),
- "ignoring parent NSEC3");
- return (ISC_R_IGNORE);
- }
- /*
- * Potential closest encloser.
- */
- if (order == 0) {
- if (closest != NULL &&
- (dns_name_countlabels(closest) == 0 ||
- dns_name_issubdomain(qname, closest)) &&
- !dns_nsec3_typepresent(&rdata, dns_rdatatype_ds) &&
- !dns_nsec3_typepresent(&rdata, dns_rdatatype_dname) &&
- (dns_nsec3_typepresent(&rdata, dns_rdatatype_soa) ||
- !dns_nsec3_typepresent(&rdata, dns_rdatatype_ns)))
- {
- dns_name_format(qname, namebuf,
- sizeof(namebuf));
- validator_log(val, ISC_LOG_DEBUG(3),
- "NSEC3 indicates potential "
- "closest encloser: '%s'",
- namebuf);
- dns_name_copy(qname, closest, NULL);
- *setclosest = ISC_TRUE;
- }
- dns_name_format(qname, namebuf, sizeof(namebuf));
- validator_log(val, ISC_LOG_DEBUG(3),
- "NSEC3 at super-domain %s", namebuf);
- return (answer);
- }
- /*
- * Find if the name does not exist.
- *
- * We continue as we need to find the name closest to the
- * closest encloser that doesn't exist.
- *
- * We also need to continue to ensure that we are not
- * proving the non-existence of a record in a sub-zone.
- * If that would be the case we will return ISC_R_IGNORE
- * above.
- */
- if ((scope < 0 && order > 0 &&
- memcmp(hash, nsec3.next, length) < 0) ||
- (scope >= 0 && (order > 0 ||
- memcmp(hash, nsec3.next, length) < 0)))
- {
- char namebuf[DNS_NAME_FORMATSIZE];
- dns_name_format(qname, namebuf, sizeof(namebuf));
- validator_log(val, ISC_LOG_DEBUG(3), "NSEC3 proves "
- "name does not exist: '%s'", namebuf);
- if (nearest != NULL &&
- (dns_name_countlabels(nearest) == 0 ||
- dns_name_issubdomain(nearest, qname))) {
- dns_name_copy(qname, nearest, NULL);
- *setnearest = ISC_TRUE;
- }
- *exists = ISC_FALSE;
- *data = ISC_FALSE;
- if (optout != NULL) {
- if ((nsec3.flags & DNS_NSEC3FLAG_OPTOUT) != 0)
- validator_log(val, ISC_LOG_DEBUG(3),
- "NSEC3 indicates optout");
- *optout =
- ISC_TF(nsec3.flags & DNS_NSEC3FLAG_OPTOUT);
- }
- answer = ISC_R_SUCCESS;
- }
- qlabels--;
- if (qlabels > 0)
- dns_name_split(qname, qlabels, NULL, qname);
- first = ISC_FALSE;
- }
- return (answer);
- }
- /*%
- * Callback for when NSEC records have been validated.
- *
- * Looks for NOQNAME, NODATA and OPTOUT proofs.
- *
- * Resumes nsecvalidate.
- */
- static void
- authvalidated(isc_task_t *task, isc_event_t *event) {
- dns_validatorevent_t *devent;
- dns_validator_t *val;
- dns_rdataset_t *rdataset;
- isc_boolean_t want_destroy;
- isc_result_t result;
- isc_boolean_t exists, data;
- UNUSED(task);
- INSIST(event->ev_type == DNS_EVENT_VALIDATORDONE);
- devent = (dns_validatorevent_t *)event;
- rdataset = devent->rdataset;
- val = devent->ev_arg;
- result = devent->result;
- dns_validator_destroy(&val->subvalidator);
- INSIST(val->event != NULL);
- validator_log(val, ISC_LOG_DEBUG(3), "in authvalidated");
- LOCK(&val->lock);
- if (CANCELED(val)) {
- validator_done(val, ISC_R_CANCELED);
- } else if (result != ISC_R_SUCCESS) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "authvalidated: got %s",
- isc_result_totext(result));
- if (result == DNS_R_BROKENCHAIN)
- val->authfail++;
- if (result == ISC_R_CANCELED)
- validator_done(val, result);
- else {
- result = nsecvalidate(val, ISC_TRUE);
- if (result != DNS_R_WAIT)
- validator_done(val, result);
- }
- } else {
- dns_name_t **proofs = val->event->proofs;
- dns_name_t *wild = dns_fixedname_name(&val->wild);
- if (rdataset->trust == dns_trust_secure)
- val->seensig = ISC_TRUE;
- if (rdataset->type == dns_rdatatype_nsec &&
- rdataset->trust == dns_trust_secure &&
- (NEEDNODATA(val) || NEEDNOQNAME(val)) &&
- !FOUNDNODATA(val) && !FOUNDNOQNAME(val) &&
- nsecnoexistnodata(val, val->event->name, devent->name,
- rdataset, &exists, &data, wild)
- == ISC_R_SUCCESS)
- {
- if (exists && !data) {
- val->attributes |= VALATTR_FOUNDNODATA;
- if (NEEDNODATA(val))
- proofs[DNS_VALIDATOR_NODATAPROOF] =
- devent->name;
- }
- if (!exists) {
- val->attributes |= VALATTR_FOUNDNOQNAME;
- val->attributes |= VALATTR_FOUNDCLOSEST;
- /*
- * The NSEC noqname proof also contains
- * the closest encloser.
- */
- if (NEEDNOQNAME(val))
- proofs[DNS_VALIDATOR_NOQNAMEPROOF] =
- devent->name;
- }
- }
- result = nsecvalidate(val, ISC_TRUE);
- if (result != DNS_R_WAIT)
- validator_done(val, result);
- }
- want_destroy = exit_check(val);
- UNLOCK(&val->lock);
- if (want_destroy)
- destroy(val);
- /*
- * Free stuff from the event.
- */
- isc_event_free(&event);
- }
- /*%
- * Looks for the requested name and type in the view (zones and cache).
- *
- * When looking for a DLV record also checks to make sure the NSEC record
- * returns covers the query name as part of aggressive negative caching.
- *
- * Returns:
- * \li ISC_R_SUCCESS
- * \li ISC_R_NOTFOUND
- * \li DNS_R_NCACHENXDOMAIN
- * \li DNS_R_NCACHENXRRSET
- * \li DNS_R_NXRRSET
- * \li DNS_R_NXDOMAIN
- * \li DNS_R_BROKENCHAIN
- */
- static inline isc_result_t
- view_find(dns_validator_t *val, dns_name_t *name, dns_rdatatype_t type) {
- dns_fixedname_t fixedname;
- dns_name_t *foundname;
- dns_rdata_nsec_t nsec;
- dns_rdata_t rdata = DNS_RDATA_INIT;
- isc_result_t result;
- unsigned int options;
- isc_time_t now;
- char buf1[DNS_NAME_FORMATSIZE];
- char buf2[DNS_NAME_FORMATSIZE];
- char buf3[DNS_NAME_FORMATSIZE];
- char namebuf[DNS_NAME_FORMATSIZE];
- char typebuf[DNS_RDATATYPE_FORMATSIZE];
- if (dns_rdataset_isassociated(&val->frdataset))
- dns_rdataset_disassociate(&val->frdataset);
- if (dns_rdataset_isassociated(&val->fsigrdataset))
- dns_rdataset_disassociate(&val->fsigrdataset);
- if (val->view->zonetable == NULL)
- return (ISC_R_CANCELED);
- if (isc_time_now(&now) == ISC_R_SUCCESS &&
- dns_resolver_getbadcache(val->view->resolver, name, type, &now)) {
- dns_name_format(name, namebuf, sizeof(namebuf));
- dns_rdatatype_format(type, typebuf, sizeof(typebuf));
- validator_log(val, ISC_LOG_INFO, "bad cache hit (%s/%s)",
- namebuf, typebuf);
- return (DNS_R_BROKENCHAIN);
- }
- options = DNS_DBFIND_PENDINGOK;
- if (type == dns_rdatatype_dlv)
- options |= DNS_DBFIND_COVERINGNSEC;
- dns_fixedname_init(&fixedname);
- foundname = dns_fixedname_name(&fixedname);
- result = dns_view_find(val->view, name, type, 0, options,
- ISC_FALSE, NULL, NULL, foundname,
- &val->frdataset, &val->fsigrdataset);
- if (result == DNS_R_NXDOMAIN) {
- if (dns_rdataset_isassociated(&val->frdataset))
- dns_rdataset_disassociate(&val->frdataset);
- if (dns_rdataset_isassociated(&val->fsigrdataset))
- dns_rdataset_disassociate(&val->fsigrdataset);
- } else if (result == DNS_R_COVERINGNSEC) {
- validator_log(val, ISC_LOG_DEBUG(3), "DNS_R_COVERINGNSEC");
- /*
- * Check if the returned NSEC covers the name.
- */
- INSIST(type == dns_rdatatype_dlv);
- if (val->frdataset.trust != dns_trust_secure) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "covering nsec: trust %s",
- dns_trust_totext(val->frdataset.trust));
- goto notfound;
- }
- result = dns_rdataset_first(&val->frdataset);
- if (result != ISC_R_SUCCESS)
- goto notfound;
- dns_rdataset_current(&val->frdataset, &rdata);
- if (dns_nsec_typepresent(&rdata, dns_rdatatype_ns) &&
- !dns_nsec_typepresent(&rdata, dns_rdatatype_soa)) {
- /* Parent NSEC record. */
- if (dns_name_issubdomain(name, foundname)) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "covering nsec: for parent");
- goto notfound;
- }
- }
- result = dns_rdata_tostruct(&rdata, &nsec, NULL);
- if (result != ISC_R_SUCCESS)
- goto notfound;
- if (dns_name_compare(foundname, &nsec.next) >= 0) {
- /* End of zone chain. */
- if (!dns_name_issubdomain(name, &nsec.next)) {
- /*
- * XXXMPA We could look for a parent NSEC
- * at nsec.next and if found retest with
- * this NSEC.
- */
- dns_rdata_freestruct(&nsec);
- validator_log(val, ISC_LOG_DEBUG(3),
- "covering nsec: not in zone");
- goto notfound;
- }
- } else if (dns_name_compare(name, &nsec.next) >= 0) {
- /*
- * XXXMPA We could check if this NSEC is at a zone
- * apex and if the qname is not below it and look for
- * a parent NSEC with the same name. This requires
- * that we can cache both NSEC records which we
- * currently don't support.
- */
- dns_rdata_freestruct(&nsec);
- validator_log(val, ISC_LOG_DEBUG(3),
- "covering nsec: not in range");
- goto notfound;
- }
- if (isc_log_wouldlog(dns_lctx,ISC_LOG_DEBUG(3))) {
- dns_name_format(name, buf1, sizeof buf1);
- dns_name_format(foundname, buf2, sizeof buf2);
- dns_name_format(&nsec.next, buf3, sizeof buf3);
- validator_log(val, ISC_LOG_DEBUG(3),
- "covering nsec found: '%s' '%s' '%s'",
- buf1, buf2, buf3);
- }
- if (dns_rdataset_isassociated(&val->frdataset))
- dns_rdataset_disassociate(&val->frdataset);
- if (dns_rdataset_isassociated(&val->fsigrdataset))
- dns_rdataset_disassociate(&val->fsigrdataset);
- dns_rdata_freestruct(&nsec);
- result = DNS_R_NCACHENXDOMAIN;
- } else if (result != ISC_R_SUCCESS &&
- result != DNS_R_NCACHENXDOMAIN &&
- result != DNS_R_NCACHENXRRSET &&
- result != DNS_R_EMPTYNAME &&
- result != DNS_R_NXRRSET &&
- result != ISC_R_NOTFOUND) {
- goto notfound;
- }
- return (result);
- notfound:
- if (dns_rdataset_isassociated(&val->frdataset))
- dns_rdataset_disassociate(&val->frdataset);
- if (dns_rdataset_isassociated(&val->fsigrdataset))
- dns_rdataset_disassociate(&val->fsigrdataset);
- return (ISC_R_NOTFOUND);
- }
- /*%
- * Checks to make sure we are not going to loop. As we use a SHARED fetch
- * the validation process will stall if looping was to occur.
- */
- static inline isc_boolean_t
- check_deadlock(dns_validator_t *val, dns_name_t *name, dns_rdatatype_t type,
- dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset)
- {
- dns_validator_t *parent;
- for (parent = val; parent != NULL; parent = parent->parent) {
- if (parent->event != NULL &&
- parent->event->type == type &&
- dns_name_equal(parent->event->name, name) &&
- /*
- * As NSEC3 records are meta data you sometimes
- * need to prove a NSEC3 record which says that
- * itself doesn't exist.
- */
- (parent->event->type != dns_rdatatype_nsec3 ||
- rdataset == NULL || sigrdataset == NULL ||
- parent->event->message == NULL ||
- parent->event->rdataset != NULL ||
- parent->event->sigrdataset != NULL))
- {
- validator_log(val, ISC_LOG_DEBUG(3),
- "continuing validation would lead to "
- "deadlock: aborting validation");
- return (ISC_TRUE);
- }
- }
- return (ISC_FALSE);
- }
- /*%
- * Start a fetch for the requested name and type.
- */
- static inline isc_result_t
- create_fetch(dns_validator_t *val, dns_name_t *name, dns_rdatatype_t type,
- isc_taskaction_t callback, const char *caller)
- {
- if (dns_rdataset_isassociated(&val->frdataset))
- dns_rdataset_disassociate(&val->frdataset);
- if (dns_rdataset_isassociated(&val->fsigrdataset))
- dns_rdataset_disassociate(&val->fsigrdataset);
- if (check_deadlock(val, name, type, NULL, NULL)) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "deadlock found (create_fetch)");
- return (DNS_R_NOVALIDSIG);
- }
- validator_logcreate(val, name, type, caller, "fetch");
- return (dns_resolver_createfetch(val->view->resolver, name, type,
- NULL, NULL, NULL, 0,
- val->event->ev_sender,
- callback, val,
- &val->frdataset,
- &val->fsigrdataset,
- &val->fetch));
- }
- /*%
- * Start a subvalidation process.
- */
- static inline isc_result_t
- create_validator(dns_validator_t *val, dns_name_t *name, dns_rdatatype_t type,
- dns_rdataset_t *rdataset, dns_rdataset_t *sigrdataset,
- isc_taskaction_t action, const char *caller)
- {
- isc_result_t result;
- if (check_deadlock(val, name, type, rdataset, sigrdataset)) {
- validator_log(val, ISC_LOG_DEBUG(3),
- "deadlock found (create_validator)");
- return (DNS_R_NOVALIDSIG);
- }
- validator_logcreate(val, name, type, caller, "validator");
- result = dns_validator_create(val->view, name, type,
- rdataset, sigrdataset, NULL, 0,
- val->task, action, val,
- &val->subvalidator);
- if (result == ISC_R_SUCCESS) {
- val->subvalidator->parent = val;
- val->subvalidator->depth = val->depth + 1;
- }
- return (result);
- }
- /*%
- * Try to find a key that could have signed 'siginfo' among those
- * in 'rdataset'. If found, build a dst_key_t for it and point
- * val->key at it.
- *
- * If val->key is non-NULL, this returns the next matching key.
- */
- static isc_result_t
- get_dst_key(dns_validator_t *val, dns_rdata_rrsig_t *siginfo,
- dns_rdataset_t *rdataset)
- {
- isc_result_t result;
- isc_buffer_t b;
- dns_rdata_t rdata = DNS_RDATA_INIT;
- dst_key_t *oldkey = val->key;
- isc_boolean_t foundold;
- if (oldkey == NULL)
- foundold = ISC_TRUE;
- else {
- foundold = ISC_FALSE;
- val->key = NULL;
- }
- result = dns_rdataset_first(rdataset);
- if (result != ISC_R_SUCCESS)
- goto failure;
- do {
- dns_rdataset_current(rdataset, &rdata);
- isc_buffer_init(&b, rdata.data, rdata.length);
- isc_buffer_add(&b, rdata.length);
- INSIST(val->key == NULL);
- result = dst_key_fromdns(&siginfo->signer, rdata.rdclass, &b,
- val->view->mctx, &val->key);
- if (result != ISC_R_SUCCESS)
- goto failure;
- if (siginfo->algorithm ==
- (dns_secalg_t)dst_key_alg(val->key) &&
- siginfo->keyid ==
- (dns_keytag_t)dst_key_id(val->key) &&
- dst_key_iszonekey(val->key))
- {
- if (foundold)
- /*
- * This is the key we're looking for.
- */
- return (ISC_R_SUCCESS);
- else if (dst_key_compare(oldkey, val->key) == ISC_TRUE)
- {
- foundold = ISC_TRUE;
- dst_key_free(&oldkey);
- }
- }
- dst_key_free(&val->key);
- dns_rdata_reset(&rdata);
- result = dns_rdataset_next(rdataset);
- } while (result == ISC_R_SUCCESS);
- if (result == ISC_R_NOMORE)
- result = ISC_R_NOTFOUND;
- failure:
- if (oldkey != NULL)
- dst_key_free(&oldkey);
- return (result);
- }
- /*%
- * Get the key that generated this signature.
- */
- static isc_result_t
- get_key(dns_validator_t *val, dns_rdata_rrsig_t *siginfo) {
- isc_result_t result;
- unsigned int nlabels;
- int order;
- dns_namereln_t namereln;
- /*
- * Is the signer name appropriate for this signature?
- *
- * The signer name must be at the same level as the owner name
- * or closer to the DNS root.
- */
- namereln = dns_name_fullcompare(val->event->name, &siginfo->signer,
- &order, &nlabels);
- if (namereln != dns_namereln_subdomain &&
- namereln != dns…