HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/resolve/resolved-dns-transaction.c

163,582 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 7acad458341057b625463484a08179dca3da17c4

Lines 3001–3879 of 3,879

                                        return r;
                                if (r == 0) {
                                        if (rr->key->type == DNS_TYPE_DS)
                                                return true;
 
                                        /* A CNAME/DNAME without a parent? That's sooo weird. */
                                        return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
                                                               "Transaction %" PRIu16 " claims CNAME/DNAME at root. Refusing.", t->id);
                                }
                        }
 
                        r = dns_name_endswith(parent, dns_resource_key_name(dns_transaction_key(dt)));
                        if (r < 0)
                                return r;
                        if (r == 0)
                                continue;
 
                        if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
                                return false;
 
                        /* We expect this to be signed when the DS record exists, and don't expect it to be
                         * signed when the DS record is proven not to exist. */
                        return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL);
                }
 
                return true;
        }
 
        default: {
                DnsTransaction *dt;
 
                /* Any other kind of RR (including DNSKEY/NSEC/NSEC3). Let's see if our DS lookup was authenticated */
 
                SET_FOREACH(dt, t->dnssec_transactions) {
                        if (dns_transaction_key(dt)->class != rr->key->class)
                                continue;
                        if (dns_transaction_key(dt)->type != DNS_TYPE_DS)
                                continue;
 
                        r = dns_name_endswith(dns_resource_key_name(rr->key), dns_resource_key_name(dns_transaction_key(dt)));
                        if (r < 0)
                                return r;
                        if (r == 0)
                                continue;
 
                        if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
                                return false;
 
                        /* We expect this to be signed when the DS record exists, and don't expect it to be
                         * signed when the DS record is proven not to exist. */
                        return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL);
                }
 
                return true;
        }}
}
 
static int dns_transaction_in_private_tld(DnsTransaction *t, const DnsResourceKey *key) {
        DnsTransaction *dt;
        const char *tld;
        int r;
 
        /* If DNSSEC downgrade mode is on, checks whether the
         * specified RR is one level below a TLD we have proven not to
         * exist. In such a case we assume that this is a private
         * domain, and permit it.
         *
         * This detects cases like the Fritz!Box router networks. Each
         * Fritz!Box router serves a private "fritz.box" zone, in the
         * non-existing TLD "box". Requests for the "fritz.box" domain
         * are served by the router itself, while requests for the
         * "box" domain will result in NXDOMAIN.
         *
         * Note that this logic is unable to detect cases where a
         * router serves a private DNS zone directly under
         * non-existing TLD. In such a case we cannot detect whether
         * the TLD is supposed to exist or not, as all requests we
         * make for it will be answered by the router's zone, and not
         * by the root zone. */
 
        assert(t);
 
        if (t->scope->dnssec_mode != DNSSEC_ALLOW_DOWNGRADE)
                return false; /* In strict DNSSEC mode what doesn't exist, doesn't exist */
 
        tld = dns_resource_key_name(key);
        r = dns_name_parent(&tld);
        if (r < 0)
                return r;
        if (r == 0)
                return false; /* Already the root domain */
 
        if (!dns_name_is_single_label(tld))
                return false;
 
        SET_FOREACH(dt, t->dnssec_transactions) {
 
                if (dns_transaction_key(dt)->class != key->class)
                        continue;
 
                r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), tld);
                if (r < 0)
                        return r;
                if (r == 0)
                        continue;
 
                /* We found an auxiliary lookup we did for the TLD. If
                 * that returned with NXDOMAIN, we know the TLD didn't
                 * exist, and hence this might be a private zone. */
 
                return dt->answer_rcode == DNS_RCODE_NXDOMAIN;
        }
 
        return false;
}
 
static int dns_transaction_requires_nsec(DnsTransaction *t) {
        char key_str[DNS_RESOURCE_KEY_STRING_MAX];
        DnsTransaction *dt;
        const char *name;
        int r;
 
        assert(t);
 
        /* Checks if we need to insist on NSEC/NSEC3 RRs for proving
         * this negative reply */
 
        if (t->scope->dnssec_mode == DNSSEC_NO)
                return false;
 
        if (dns_type_is_pseudo(dns_transaction_key(t)->type))
                return -EINVAL;
 
        r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(dns_transaction_key(t)));
        if (r < 0)
                return r;
        if (r > 0)
                return false;
 
        r = dns_transaction_in_private_tld(t, dns_transaction_key(t));
        if (r < 0)
                return r;
        if (r > 0) {
                /* The lookup is from a TLD that is proven not to
                 * exist, and we are in downgrade mode, hence ignore
                 * that fact that we didn't get any NSEC RRs. */
 
                log_info("Detected a negative query %s in a private DNS zone, permitting unsigned response.",
                         dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str));
                return false;
        }
 
        name = dns_resource_key_name(dns_transaction_key(t));
 
        /* For all other RRs we check the DS on the same level to see
         * if it's signed. */
 
        SET_FOREACH(dt, t->dnssec_transactions) {
                if (dns_transaction_key(dt)->class != dns_transaction_key(t)->class)
                        continue;
                if (dns_transaction_key(dt)->type != DNS_TYPE_DS)
                        continue;
 
                r = dns_name_endswith(name, dns_resource_key_name(dns_transaction_key(dt)));
                if (r < 0)
                        return r;
                if (r == 0)
                        continue;
 
                if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
                        return false;
 
                /* We expect this to be signed when the DS record exists, and don't expect it to be signed
                 * when the DS record is proven not to exist. */
                return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL);
        }
 
        /* If in doubt, require NSEC/NSEC3 */
        return true;
}
 
static int dns_transaction_dnskey_authenticated(DnsTransaction *t, DnsResourceRecord *rr) {
        DnsResourceRecord *rrsig;
        bool found = false;
        int r;
 
        /* Checks whether any of the DNSKEYs used for the RRSIGs for
         * the specified RRset is authenticated (i.e. has a matching
         * DS RR). */
 
        r = dns_transaction_negative_trust_anchor_lookup(t, dns_resource_key_name(rr->key));
        if (r < 0)
                return r;
        if (r > 0)
                return false;
 
        DNS_ANSWER_FOREACH(rrsig, t->answer) {
                DnsTransaction *dt;
 
                r = dnssec_key_match_rrsig(rr->key, rrsig);
                if (r < 0)
                        return r;
                if (r == 0)
                        continue;
 
                SET_FOREACH(dt, t->dnssec_transactions) {
 
                        if (dns_transaction_key(dt)->class != rr->key->class)
                                continue;
 
                        if (dns_transaction_key(dt)->type == DNS_TYPE_DNSKEY) {
 
                                r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), rrsig->rrsig.signer);
                                if (r < 0)
                                        return r;
                                if (r == 0)
                                        continue;
 
                                /* OK, we found an auxiliary DNSKEY lookup. If that lookup is authenticated,
                                 * report this. */
 
                                if (FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
                                        return true;
 
                                found = true;
 
                        } else if (dns_transaction_key(dt)->type == DNS_TYPE_DS) {
 
                                r = dns_name_equal(dns_resource_key_name(dns_transaction_key(dt)), rrsig->rrsig.signer);
                                if (r < 0)
                                        return r;
                                if (r == 0)
                                        continue;
 
                                /* OK, we found an auxiliary DS lookup. If that lookup is authenticated and
                                 * non-zero, we won! */
 
                                if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
                                        return false;
 
                                return dns_answer_match_key(dt->answer, dns_transaction_key(dt), NULL);
                        }
                }
        }
 
        return found ? false : -ENXIO;
}
 
static int dns_transaction_known_signed(DnsTransaction *t, DnsResourceRecord *rr) {
        assert(t);
        assert(rr);
 
        /* We know that the root domain is signed, hence if it appears
         * not to be signed, there's a problem with the DNS server */
 
        return rr->key->class == DNS_CLASS_IN &&
                dns_name_is_root(dns_resource_key_name(rr->key));
}
 
static int dns_transaction_check_revoked_trust_anchors(DnsTransaction *t) {
        DnsResourceRecord *rr;
        int r;
 
        assert(t);
 
        /* Maybe warn the user that we encountered a revoked DNSKEY
         * for a key from our trust anchor. Note that we don't care
         * whether the DNSKEY can be authenticated or not. It's
         * sufficient if it is self-signed. */
 
        DNS_ANSWER_FOREACH(rr, t->answer) {
                r = dns_trust_anchor_check_revoked(&t->scope->manager->trust_anchor, rr, t->answer);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
static int dns_transaction_invalidate_revoked_keys(DnsTransaction *t) {
        bool changed;
        int r;
 
        assert(t);
 
        /* Removes all DNSKEY/DS objects from t->validated_keys that
         * our trust anchors database considers revoked. */
 
        do {
                DnsResourceRecord *rr;
 
                changed = false;
 
                DNS_ANSWER_FOREACH(rr, t->validated_keys) {
                        r = dns_trust_anchor_is_revoked(&t->scope->manager->trust_anchor, rr);
                        if (r < 0)
                                return r;
                        if (r > 0) {
                                r = dns_answer_remove_by_rr(&t->validated_keys, rr);
                                if (r < 0)
                                        return r;
 
                                assert(r > 0);
                                changed = true;
                                break;
                        }
                }
        } while (changed);
 
        return 0;
}
 
static int dns_transaction_copy_validated(DnsTransaction *t) {
        DnsTransaction *dt;
        int r;
 
        assert(t);
 
        /* Copy all validated RRs from the auxiliary DNSSEC transactions into our set of validated RRs */
 
        SET_FOREACH(dt, t->dnssec_transactions) {
 
                if (DNS_TRANSACTION_IS_LIVE(dt->state))
                        continue;
 
                /* Some of the validated keys may not be authenticated, but are still useful to report
                 * insecure answers when the domain is signed only by unsupported algorithms. */
                r = dns_answer_extend(&t->validated_keys, dt->validated_keys);
                if (r < 0)
                        return r;
 
                if (!FLAGS_SET(dt->answer_query_flags, SD_RESOLVED_AUTHENTICATED))
                        continue;
 
                r = dns_answer_extend(&t->validated_keys, dt->answer);
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
typedef enum {
        DNSSEC_PHASE_DNSKEY,   /* Phase #1, only validate DNSKEYs */
        DNSSEC_PHASE_NSEC,     /* Phase #2, only validate NSEC+NSEC3 */
        DNSSEC_PHASE_ALL,      /* Phase #3, validate everything else */
} Phase;
 
static int dnssec_validate_records(
                DnsTransaction *t,
                Phase phase,
                bool *have_nsec,
                unsigned *nvalidations,
                DnsAnswer **validated) {
 
        DnsResourceRecord *rr;
        int r;
 
        assert(have_nsec);
        assert(nvalidations);
        assert(validated);
 
        /* Returns negative on error, 0 if validation failed, 1 to restart validation, 2 when finished. */
 
        DNS_ANSWER_FOREACH(rr, t->answer) {
                _unused_ _cleanup_(dns_resource_record_unrefp) DnsResourceRecord *rr_ref = dns_resource_record_ref(rr);
                DnsResourceRecord *rrsig = NULL;
                DnssecResult result;
 
                switch (rr->key->type) {
                case DNS_TYPE_RRSIG:
                        continue;
 
                case DNS_TYPE_DNSKEY:
                        /* We validate DNSKEYs only in the DNSKEY and ALL phases */
                        if (phase == DNSSEC_PHASE_NSEC)
                                continue;
                        break;
 
                case DNS_TYPE_NSEC:
                case DNS_TYPE_NSEC3:
                        *have_nsec = true;
 
                        /* We validate NSEC/NSEC3 only in the NSEC and ALL phases */
                        if (phase == DNSSEC_PHASE_DNSKEY)
                                continue;
                        break;
 
                default:
                        /* We validate all other RRs only in the ALL phases */
                        if (phase != DNSSEC_PHASE_ALL)
                                continue;
                }
 
                r = dnssec_verify_rrset_search(
                                t->answer,
                                rr->key,
                                t->validated_keys,
                                USEC_INFINITY,
                                &result,
                                &rrsig);
                if (r < 0)
                        return r;
                *nvalidations += r;
 
                log_debug("Looking at %s: %s", strna(dns_resource_record_to_string(rr)), dnssec_result_to_string(result));
 
                if (result == DNSSEC_VALIDATED) {
                        assert(rrsig);
 
                        if (rr->key->type == DNS_TYPE_DNSKEY) {
                                /* If we just validated a DNSKEY RRset, then let's add these keys to
                                 * the set of validated keys for this transaction. */
 
                                r = dns_answer_copy_by_key(&t->validated_keys, t->answer, rr->key, DNS_ANSWER_AUTHENTICATED, rrsig);
                                if (r < 0)
                                        return r;
 
                                /* Some of the DNSKEYs we just added might already have been revoked,
                                 * remove them again in that case. */
                                r = dns_transaction_invalidate_revoked_keys(t);
                                if (r < 0)
                                        return r;
                        }
 
                        /* Add the validated RRset to the new list of validated RRsets, and remove it from
                         * the unvalidated RRsets.  We mark the RRset as authenticated and cacheable. */
                        r = dns_answer_move_by_key(validated, &t->answer, rr->key, DNS_ANSWER_AUTHENTICATED|DNS_ANSWER_CACHEABLE, rrsig);
                        if (r < 0)
                                return r;
 
                        manager_dnssec_verdict(t->scope->manager, DNSSEC_SECURE, rr->key);
 
                        /* Exit the loop, we dropped something from the answer, start from the beginning */
                        return 1;
                }
 
                /* If we haven't read all DNSKEYs yet a negative result of the validation is irrelevant, as
                 * there might be more DNSKEYs coming. Similar, if we haven't read all NSEC/NSEC3 RRs yet,
                 * we cannot do positive wildcard proofs yet, as those require the NSEC/NSEC3 RRs. */
                if (phase != DNSSEC_PHASE_ALL)
                        continue;
 
                if (result == DNSSEC_VALIDATED_WILDCARD) {
                        bool authenticated = false;
                        const char *source;
 
                        assert(rrsig);
 
                        /* This RRset validated, but as a wildcard. This means we need
                         * to prove via NSEC/NSEC3 that no matching non-wildcard RR exists. */
 
                        /* First step, determine the source of synthesis */
                        r = dns_resource_record_source(rrsig, &source);
                        if (r < 0)
                                return r;
 
                        r = dnssec_test_positive_wildcard(*validated,
                                                          dns_resource_key_name(rr->key),
                                                          source,
                                                          rrsig->rrsig.signer,
                                                          &authenticated);
 
                        /* Unless the NSEC proof showed that the key really doesn't exist something is off. */
                        if (r == 0)
                                result = DNSSEC_INVALID;
                        else {
                                r = dns_answer_move_by_key(
                                                validated,
                                                &t->answer,
                                                rr->key,
                                                authenticated ? (DNS_ANSWER_AUTHENTICATED|DNS_ANSWER_CACHEABLE) : 0,
                                                rrsig);
                                if (r < 0)
                                        return r;
 
                                manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, rr->key);
 
                                /* Exit the loop, we dropped something from the answer, start from the beginning */
                                return 1;
                        }
                }
 
                if (result == DNSSEC_NO_SIGNATURE) {
                        r = dns_transaction_requires_rrsig(t, rr);
                        if (r < 0)
                                return r;
                        if (r == 0) {
                                /* Data does not require signing. In that case, just copy it over,
                                 * but remember that this is by no means authenticated. */
                                r = dns_answer_move_by_key(
                                                validated,
                                                &t->answer,
                                                rr->key,
                                                0,
                                                NULL);
                                if (r < 0)
                                        return r;
 
                                manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
                                return 1;
                        }
 
                        r = dns_transaction_known_signed(t, rr);
                        if (r < 0)
                                return r;
                        if (r > 0) {
                                /* This is an RR we know has to be signed. If it isn't this means
                                 * the server is not attaching RRSIGs, hence complain. */
 
                                dns_server_packet_rrsig_missing(t->server, t->current_feature_level);
 
                                if (t->scope->dnssec_mode == DNSSEC_ALLOW_DOWNGRADE) {
 
                                        /* Downgrading is OK? If so, just consider the information unsigned */
 
                                        r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL);
                                        if (r < 0)
                                                return r;
 
                                        manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
                                        return 1;
                                }
 
                                /* Otherwise, fail */
                                t->answer_dnssec_result = DNSSEC_INCOMPATIBLE_SERVER;
                                return 0;
                        }
 
                        r = dns_transaction_in_private_tld(t, rr->key);
                        if (r < 0)
                                return r;
                        if (r > 0) {
                                char s[DNS_RESOURCE_KEY_STRING_MAX];
 
                                /* The data is from a TLD that is proven not to exist, and we are in downgrade
                                 * mode, hence ignore the fact that this was not signed. */
 
                                log_info("Detected RRset %s is in a private DNS zone, permitting unsigned RRs.",
                                         dns_resource_key_to_string(rr->key, s, sizeof s));
 
                                r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL);
                                if (r < 0)
                                        return r;
 
                                manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
                                return 1;
                        }
                }
 
                /* https://datatracker.ietf.org/doc/html/rfc6840#section-5.2 */
                if (result == DNSSEC_UNSUPPORTED_ALGORITHM) {
                        if (rr->key->type == DNS_TYPE_DNSKEY) {
                                /* This is a DNSKEY we cannot authenticate, but it might still be the best
                                 * offer from the resolver. Add it to the validated keys in case it's the
                                 * best we can find, but do not mark it as authenticated.
                                 */
 
                                r = dns_answer_copy_by_key(&t->validated_keys, t->answer, rr->key, 0, NULL);
                                if (r < 0)
                                        return r;
 
                                /* Some of the DNSKEYs we just added might already have been revoked,
                                 * remove them again in that case. */
                                r = dns_transaction_invalidate_revoked_keys(t);
                                if (r < 0)
                                        return r;
                        }
 
                        r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL);
                        if (r < 0)
                                return r;
 
                        manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
                        return 1;
                }
 
                if (IN_SET(result,
                           DNSSEC_MISSING_KEY,
                           DNSSEC_SIGNATURE_EXPIRED)) {
 
                        r = dns_transaction_dnskey_authenticated(t, rr);
                        if (r < 0 && r != -ENXIO)
                                return r;
                        if (r == 0) {
                                /* The DNSKEY transaction was not authenticated, this means there's
                                 * no DS for this, which means it's OK if no keys are found for this signature. */
 
                                r = dns_answer_move_by_key(validated, &t->answer, rr->key, 0, NULL);
                                if (r < 0)
                                        return r;
 
                                manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, rr->key);
                                return 1;
                        }
                }
 
                r = dns_transaction_is_primary_response(t, rr);
                if (r < 0)
                        return r;
                if (r > 0) {
                        /* Look for a matching DNAME for this CNAME */
                        r = dns_answer_has_dname_for_cname(t->answer, rr);
                        if (r < 0)
                                return r;
                        if (r == 0) {
                                /* Also look among the stuff we already validated */
                                r = dns_answer_has_dname_for_cname(*validated, rr);
                                if (r < 0)
                                        return r;
                        }
 
                        if (r == 0) {
                                if (IN_SET(result,
                                           DNSSEC_INVALID,
                                           DNSSEC_SIGNATURE_EXPIRED,
                                           DNSSEC_NO_SIGNATURE))
                                        manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, rr->key);
                                else /* DNSSEC_MISSING_KEY, DNSSEC_UNSUPPORTED_ALGORITHM,
                                        or DNSSEC_TOO_MANY_VALIDATIONS */
                                        manager_dnssec_verdict(t->scope->manager, DNSSEC_INDETERMINATE, rr->key);
 
                                /* This is a primary response to our question, and it failed validation.
                                 * That's fatal. */
                                t->answer_dnssec_result = result;
                                return 0;
                        }
 
                        /* This is a primary response, but we do have a DNAME RR
                         * in the RR that can replay this CNAME, hence rely on
                         * that, and we can remove the CNAME in favour of it. */
                }
 
                /* This is just some auxiliary data. Just remove the RRset and continue. */
                r = dns_answer_remove_by_key(&t->answer, rr->key);
                if (r < 0)
                        return r;
 
                /* We dropped something from the answer, start from the beginning. */
                return 1;
        }
 
        return 2; /* Finito. */
}
 
int dns_transaction_validate_dnssec(DnsTransaction *t) {
        _cleanup_(dns_answer_unrefp) DnsAnswer *validated = NULL;
        Phase phase;
        DnsAnswerFlags flags;
        int r;
        char key_str[DNS_RESOURCE_KEY_STRING_MAX];
 
        assert(t);
 
        /* We have now collected all DS and DNSKEY RRs in t->validated_keys, let's see which RRs we can now
         * authenticate with that. */
 
        if (FLAGS_SET(t->query_flags, SD_RESOLVED_NO_VALIDATE) || t->scope->dnssec_mode == DNSSEC_NO)
                return 0;
 
        /* Already validated */
        if (t->answer_dnssec_result != _DNSSEC_RESULT_INVALID)
                return 0;
 
        /* Our own stuff needs no validation */
        if (IN_SET(t->answer_source, DNS_TRANSACTION_ZONE, DNS_TRANSACTION_TRUST_ANCHOR)) {
                t->answer_dnssec_result = DNSSEC_VALIDATED;
                SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, true);
                return 0;
        }
 
        /* Cached stuff is not affected by validation. */
        if (t->answer_source != DNS_TRANSACTION_NETWORK)
                return 0;
 
        if (!dns_transaction_dnssec_supported_full(t)) {
                /* The server does not support DNSSEC, or doesn't augment responses with RRSIGs, or this
                 * query or an auxiliary one went out without DO. */
                t->answer_dnssec_result = DNSSEC_INCOMPATIBLE_SERVER;
                log_debug("Not validating response for %" PRIu16 ", the server does not support DNSSEC, or the "
                          "query or an auxiliary one was sent without DO.", t->id);
                return 0;
        }
 
        log_debug("Validating response from transaction %" PRIu16 " (%s).",
                  t->id,
                  dns_resource_key_to_string(dns_transaction_key(t), key_str, sizeof key_str));
 
        /* First, see if this response contains any revoked trust anchors we care about. */
        r = dns_transaction_check_revoked_trust_anchors(t);
        if (r < 0)
                return r;
 
        /* Second, copy all RRs we acquired successfully from auxiliary RRs over. */
        r = dns_transaction_copy_validated(t);
        if (r < 0)
                return r;
 
        /* Third, see if there are DNSKEYs we already know a validated DS for. */
        r = dns_transaction_validate_dnskey_by_ds(t);
        if (r < 0)
                return r;
 
        /* Fourth, remove all DNSKEY and DS RRs again that our trust anchor says are revoked. After all we
         * might have marked some keys revoked above, but they might still be lingering in our validated_keys
         * list. */
        r = dns_transaction_invalidate_revoked_keys(t);
        if (r < 0)
                return r;
 
        phase = DNSSEC_PHASE_DNSKEY;
        for (unsigned nvalidations = 0;;) {
                bool have_nsec = false;
 
                r = dnssec_validate_records(t, phase, &have_nsec, &nvalidations, &validated);
                if (r <= 0) {
                        DNS_ANSWER_REPLACE(t->answer, TAKE_PTR(validated));
                        return r;
                }
 
                if (nvalidations > DNSSEC_VALIDATION_MAX) {
                        /* This reply requires an onerous number of signature validations to verify. Let's
                         * not waste our time trying, as this shouldn't happen for well-behaved domains
                         * anyway. */
                        t->answer_dnssec_result = DNSSEC_TOO_MANY_VALIDATIONS;
                        DNS_ANSWER_REPLACE(t->answer, TAKE_PTR(validated));
                        return 0;
                }
 
                /* Try again as long as we managed to achieve something */
                if (r == 1)
                        continue;
 
                if (phase == DNSSEC_PHASE_DNSKEY && have_nsec) {
                        /* OK, we processed all DNSKEYs, and there are NSEC/NSEC3 RRs, look at those now. */
                        phase = DNSSEC_PHASE_NSEC;
                        continue;
                }
 
                if (phase != DNSSEC_PHASE_ALL) {
                        /* OK, we processed all DNSKEYs and NSEC/NSEC3 RRs, look at all the rest now.
                         * Note that in this third phase we start to remove RRs we couldn't validate. */
                        phase = DNSSEC_PHASE_ALL;
                        continue;
                }
 
                /* We're done */
                break;
        }
 
        DNS_ANSWER_REPLACE(t->answer, TAKE_PTR(validated));
 
        /* At this point the answer only contains validated
         * RRsets. Now, let's see if it actually answers the question
         * we asked. If so, great! If it doesn't, then see if
         * NSEC/NSEC3 can prove this. */
        r = dns_transaction_has_positive_answer(t, &flags);
        if (r > 0) {
                /* Yes, it answers the question! */
 
                if (flags & DNS_ANSWER_AUTHENTICATED) {
                        /* The answer is fully authenticated, yay. */
                        t->answer_dnssec_result = DNSSEC_VALIDATED;
                        t->answer_rcode = DNS_RCODE_SUCCESS;
                        SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, true);
                } else {
                        /* The answer is not fully authenticated. */
                        t->answer_dnssec_result = DNSSEC_UNSIGNED;
                        SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false);
                }
 
        } else if (r == 0) {
                DnssecNsecResult nr;
                bool authenticated = false;
 
                /* Bummer! Let's check NSEC/NSEC3 */
                r = dnssec_nsec_test(t->answer, dns_transaction_key(t), &nr, &authenticated, &t->answer_nsec_ttl);
                if (r < 0)
                        return r;
 
                switch (nr) {
 
                case DNSSEC_NSEC_NXDOMAIN:
                        /* NSEC proves the domain doesn't exist. Very good. */
                        log_debug("Proved NXDOMAIN via NSEC/NSEC3 for transaction %u (%s)", t->id, key_str);
                        t->answer_dnssec_result = DNSSEC_VALIDATED;
                        t->answer_rcode = DNS_RCODE_NXDOMAIN;
                        SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, authenticated);
 
                        manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, dns_transaction_key(t));
                        break;
 
                case DNSSEC_NSEC_NODATA:
                        /* NSEC proves that there's no data here, very good. */
                        log_debug("Proved NODATA via NSEC/NSEC3 for transaction %u (%s)", t->id, key_str);
                        t->answer_dnssec_result = DNSSEC_VALIDATED;
                        t->answer_rcode = DNS_RCODE_SUCCESS;
                        SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, authenticated);
 
                        manager_dnssec_verdict(t->scope->manager, authenticated ? DNSSEC_SECURE : DNSSEC_INSECURE, dns_transaction_key(t));
                        break;
 
                case DNSSEC_NSEC_OPTOUT:
                        /* NSEC3 says the data might not be signed */
                        log_debug("Data is NSEC3 opt-out via NSEC/NSEC3 for transaction %u (%s)", t->id, key_str);
                        t->answer_dnssec_result = DNSSEC_UNSIGNED;
                        SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false);
 
                        manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, dns_transaction_key(t));
                        break;
 
                case DNSSEC_NSEC_NO_RR:
                        /* No NSEC data? Bummer! */
 
                        r = dns_transaction_requires_nsec(t);
                        if (r < 0)
                                return r;
                        if (r > 0) {
                                t->answer_dnssec_result = DNSSEC_NO_SIGNATURE;
                                manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, dns_transaction_key(t));
                        } else {
                                t->answer_dnssec_result = DNSSEC_UNSIGNED;
                                SET_FLAG(t->answer_query_flags, SD_RESOLVED_AUTHENTICATED, false);
                                manager_dnssec_verdict(t->scope->manager, DNSSEC_INSECURE, dns_transaction_key(t));
                        }
 
                        break;
 
                case DNSSEC_NSEC_UNSUPPORTED_ALGORITHM:
                        /* We don't know the NSEC3 algorithm used? */
                        t->answer_dnssec_result = DNSSEC_UNSUPPORTED_ALGORITHM;
                        manager_dnssec_verdict(t->scope->manager, DNSSEC_INDETERMINATE, dns_transaction_key(t));
                        break;
 
                case DNSSEC_NSEC_FOUND:
                case DNSSEC_NSEC_CNAME:
                        /* NSEC says it needs to be there, but we couldn't find it? Bummer! */
                        t->answer_dnssec_result = DNSSEC_NSEC_MISMATCH;
                        manager_dnssec_verdict(t->scope->manager, DNSSEC_BOGUS, dns_transaction_key(t));
                        break;
 
                default:
                        assert_not_reached();
                }
        }
 
        return 1;
}
 
static const char* const dns_transaction_state_table[_DNS_TRANSACTION_STATE_MAX] = {
        [DNS_TRANSACTION_NULL]                 = "null",
        [DNS_TRANSACTION_PENDING]              = "pending",
        [DNS_TRANSACTION_VALIDATING]           = "validating",
        [DNS_TRANSACTION_RCODE_FAILURE]        = "rcode-failure",
        [DNS_TRANSACTION_SUCCESS]              = "success",
        [DNS_TRANSACTION_NO_SERVERS]           = "no-servers",
        [DNS_TRANSACTION_TIMEOUT]              = "timeout",
        [DNS_TRANSACTION_ATTEMPTS_MAX_REACHED] = "attempts-max-reached",
        [DNS_TRANSACTION_INVALID_REPLY]        = "invalid-reply",
        [DNS_TRANSACTION_ERRNO]                = "errno",
        [DNS_TRANSACTION_ABORTED]              = "aborted",
        [DNS_TRANSACTION_DNSSEC_FAILED]        = "dnssec-failed",
        [DNS_TRANSACTION_NO_TRUST_ANCHOR]      = "no-trust-anchor",
        [DNS_TRANSACTION_RR_TYPE_UNSUPPORTED]  = "rr-type-unsupported",
        [DNS_TRANSACTION_NETWORK_DOWN]         = "network-down",
        [DNS_TRANSACTION_NOT_FOUND]            = "not-found",
        [DNS_TRANSACTION_NO_SOURCE]            = "no-source",
        [DNS_TRANSACTION_STUB_LOOP]            = "stub-loop",
};
DEFINE_STRING_TABLE_LOOKUP(dns_transaction_state, DnsTransactionState);
 
static const char* const dns_transaction_source_table[_DNS_TRANSACTION_SOURCE_MAX] = {
        [DNS_TRANSACTION_NETWORK]      = "network",
        [DNS_TRANSACTION_CACHE]        = "cache",
        [DNS_TRANSACTION_ZONE]         = "zone",
        [DNS_TRANSACTION_TRUST_ANCHOR] = "trust-anchor",
};
DEFINE_STRING_TABLE_LOOKUP(dns_transaction_source, DnsTransactionSource);