xref: /netbsd-src/external/bsd/unbound/dist/validator/val_utils.c (revision 91f7d55fb697b5e0475da4718fa34c3a3ebeac85)
13b6c3722Schristos /*
23b6c3722Schristos  * validator/val_utils.c - validator utility functions.
33b6c3722Schristos  *
43b6c3722Schristos  * Copyright (c) 2007, NLnet Labs. All rights reserved.
53b6c3722Schristos  *
63b6c3722Schristos  * This software is open source.
73b6c3722Schristos  *
83b6c3722Schristos  * Redistribution and use in source and binary forms, with or without
93b6c3722Schristos  * modification, are permitted provided that the following conditions
103b6c3722Schristos  * are met:
113b6c3722Schristos  *
123b6c3722Schristos  * Redistributions of source code must retain the above copyright notice,
133b6c3722Schristos  * this list of conditions and the following disclaimer.
143b6c3722Schristos  *
153b6c3722Schristos  * Redistributions in binary form must reproduce the above copyright notice,
163b6c3722Schristos  * this list of conditions and the following disclaimer in the documentation
173b6c3722Schristos  * and/or other materials provided with the distribution.
183b6c3722Schristos  *
193b6c3722Schristos  * Neither the name of the NLNET LABS nor the names of its contributors may
203b6c3722Schristos  * be used to endorse or promote products derived from this software without
213b6c3722Schristos  * specific prior written permission.
223b6c3722Schristos  *
233b6c3722Schristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
243b6c3722Schristos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
253b6c3722Schristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
263b6c3722Schristos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
273b6c3722Schristos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
283b6c3722Schristos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
293b6c3722Schristos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
303b6c3722Schristos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
313b6c3722Schristos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
323b6c3722Schristos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
333b6c3722Schristos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
343b6c3722Schristos  */
353b6c3722Schristos 
363b6c3722Schristos /**
373b6c3722Schristos  * \file
383b6c3722Schristos  *
393b6c3722Schristos  * This file contains helper functions for the validator module.
403b6c3722Schristos  */
413b6c3722Schristos #include "config.h"
423b6c3722Schristos #include "validator/val_utils.h"
433b6c3722Schristos #include "validator/validator.h"
443b6c3722Schristos #include "validator/val_kentry.h"
453b6c3722Schristos #include "validator/val_sigcrypt.h"
463b6c3722Schristos #include "validator/val_anchor.h"
473b6c3722Schristos #include "validator/val_nsec.h"
483b6c3722Schristos #include "validator/val_neg.h"
493b6c3722Schristos #include "services/cache/rrset.h"
503b6c3722Schristos #include "services/cache/dns.h"
513b6c3722Schristos #include "util/data/msgreply.h"
523b6c3722Schristos #include "util/data/packed_rrset.h"
533b6c3722Schristos #include "util/data/dname.h"
543b6c3722Schristos #include "util/net_help.h"
553b6c3722Schristos #include "util/module.h"
563b6c3722Schristos #include "util/regional.h"
570cd9f4ecSchristos #include "util/config_file.h"
583b6c3722Schristos #include "sldns/wire2str.h"
593b6c3722Schristos #include "sldns/parseutil.h"
603b6c3722Schristos 
61*91f7d55fSchristos /** Maximum allowed digest match failures per DS, for DNSKEYs with the same
62*91f7d55fSchristos  *  properties */
63*91f7d55fSchristos #define MAX_DS_MATCH_FAILURES 4
64*91f7d55fSchristos 
653b6c3722Schristos enum val_classification
val_classify_response(uint16_t query_flags,struct query_info * origqinf,struct query_info * qinf,struct reply_info * rep,size_t skip)663b6c3722Schristos val_classify_response(uint16_t query_flags, struct query_info* origqinf,
673b6c3722Schristos 	struct query_info* qinf, struct reply_info* rep, size_t skip)
683b6c3722Schristos {
693b6c3722Schristos 	int rcode = (int)FLAGS_GET_RCODE(rep->flags);
703b6c3722Schristos 	size_t i;
713b6c3722Schristos 
723b6c3722Schristos 	/* Normal Name Error's are easy to detect -- but don't mistake a CNAME
733b6c3722Schristos 	 * chain ending in NXDOMAIN. */
743b6c3722Schristos 	if(rcode == LDNS_RCODE_NXDOMAIN && rep->an_numrrsets == 0)
753b6c3722Schristos 		return VAL_CLASS_NAMEERROR;
763b6c3722Schristos 
773b6c3722Schristos 	/* check for referral: nonRD query and it looks like a nodata */
783b6c3722Schristos 	if(!(query_flags&BIT_RD) && rep->an_numrrsets == 0 &&
793b6c3722Schristos 		rcode == LDNS_RCODE_NOERROR) {
803b6c3722Schristos 		/* SOA record in auth indicates it is NODATA instead.
813b6c3722Schristos 		 * All validation requiring NODATA messages have SOA in
823b6c3722Schristos 		 * authority section. */
833b6c3722Schristos 		/* uses fact that answer section is empty */
843b6c3722Schristos 		int saw_ns = 0;
853b6c3722Schristos 		for(i=0; i<rep->ns_numrrsets; i++) {
863b6c3722Schristos 			if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_SOA)
873b6c3722Schristos 				return VAL_CLASS_NODATA;
883b6c3722Schristos 			if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_DS)
893b6c3722Schristos 				return VAL_CLASS_REFERRAL;
903b6c3722Schristos 			if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS)
913b6c3722Schristos 				saw_ns = 1;
923b6c3722Schristos 		}
933b6c3722Schristos 		return saw_ns?VAL_CLASS_REFERRAL:VAL_CLASS_NODATA;
943b6c3722Schristos 	}
953b6c3722Schristos 	/* root referral where NS set is in the answer section */
963b6c3722Schristos 	if(!(query_flags&BIT_RD) && rep->ns_numrrsets == 0 &&
973b6c3722Schristos 		rep->an_numrrsets == 1 && rcode == LDNS_RCODE_NOERROR &&
983b6c3722Schristos 		ntohs(rep->rrsets[0]->rk.type) == LDNS_RR_TYPE_NS &&
993b6c3722Schristos 		query_dname_compare(rep->rrsets[0]->rk.dname,
1003b6c3722Schristos 			origqinf->qname) != 0)
1013b6c3722Schristos 		return VAL_CLASS_REFERRAL;
1023b6c3722Schristos 
1033b6c3722Schristos 	/* dump bad messages */
1043b6c3722Schristos 	if(rcode != LDNS_RCODE_NOERROR && rcode != LDNS_RCODE_NXDOMAIN)
1053b6c3722Schristos 		return VAL_CLASS_UNKNOWN;
1063b6c3722Schristos 	/* next check if the skip into the answer section shows no answer */
1073b6c3722Schristos 	if(skip>0 && rep->an_numrrsets <= skip)
1083b6c3722Schristos 		return VAL_CLASS_CNAMENOANSWER;
1093b6c3722Schristos 
1103b6c3722Schristos 	/* Next is NODATA */
1113b6c3722Schristos 	if(rcode == LDNS_RCODE_NOERROR && rep->an_numrrsets == 0)
1123b6c3722Schristos 		return VAL_CLASS_NODATA;
1133b6c3722Schristos 
1143b6c3722Schristos 	/* We distinguish between CNAME response and other positive/negative
1153b6c3722Schristos 	 * responses because CNAME answers require extra processing. */
1163b6c3722Schristos 
1173b6c3722Schristos 	/* We distinguish between ANY and CNAME or POSITIVE because
1183b6c3722Schristos 	 * ANY responses are validated differently. */
1193b6c3722Schristos 	if(rcode == LDNS_RCODE_NOERROR && qinf->qtype == LDNS_RR_TYPE_ANY)
1203b6c3722Schristos 		return VAL_CLASS_ANY;
1213b6c3722Schristos 
1223b6c3722Schristos 	/* Note that DNAMEs will be ignored here, unless qtype=DNAME. Unless
1233b6c3722Schristos 	 * qtype=CNAME, this will yield a CNAME response. */
1243b6c3722Schristos 	for(i=skip; i<rep->an_numrrsets; i++) {
1253b6c3722Schristos 		if(rcode == LDNS_RCODE_NOERROR &&
1263b6c3722Schristos 			ntohs(rep->rrsets[i]->rk.type) == qinf->qtype)
1273b6c3722Schristos 			return VAL_CLASS_POSITIVE;
1283b6c3722Schristos 		if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_CNAME)
1293b6c3722Schristos 			return VAL_CLASS_CNAME;
1303b6c3722Schristos 	}
1313b6c3722Schristos 	log_dns_msg("validator: error. failed to classify response message: ",
1323b6c3722Schristos 		qinf, rep);
1333b6c3722Schristos 	return VAL_CLASS_UNKNOWN;
1343b6c3722Schristos }
1353b6c3722Schristos 
1363b6c3722Schristos /** Get signer name from RRSIG */
1373b6c3722Schristos static void
rrsig_get_signer(uint8_t * data,size_t len,uint8_t ** sname,size_t * slen)1383b6c3722Schristos rrsig_get_signer(uint8_t* data, size_t len, uint8_t** sname, size_t* slen)
1393b6c3722Schristos {
1403b6c3722Schristos 	/* RRSIG rdata is not allowed to be compressed, it is stored
1413b6c3722Schristos 	 * uncompressed in memory as well, so return a ptr to the name */
1423b6c3722Schristos 	if(len < 21) {
1433b6c3722Schristos 		/* too short RRSig:
1443b6c3722Schristos 		 * short, byte, byte, long, long, long, short, "." is
1453b6c3722Schristos 		 * 2	1	1	4	4  4	2	1 = 19
1463b6c3722Schristos 		 * 			and a skip of 18 bytes to the name.
1473b6c3722Schristos 		 * +2 for the rdatalen is 21 bytes len for root label */
1483b6c3722Schristos 		*sname = NULL;
1493b6c3722Schristos 		*slen = 0;
1503b6c3722Schristos 		return;
1513b6c3722Schristos 	}
1523b6c3722Schristos 	data += 20; /* skip the fixed size bits */
1533b6c3722Schristos 	len -= 20;
1543b6c3722Schristos 	*slen = dname_valid(data, len);
1553b6c3722Schristos 	if(!*slen) {
1563b6c3722Schristos 		/* bad dname in this rrsig. */
1573b6c3722Schristos 		*sname = NULL;
1583b6c3722Schristos 		return;
1593b6c3722Schristos 	}
1603b6c3722Schristos 	*sname = data;
1613b6c3722Schristos }
1623b6c3722Schristos 
1633b6c3722Schristos void
val_find_rrset_signer(struct ub_packed_rrset_key * rrset,uint8_t ** sname,size_t * slen)1643b6c3722Schristos val_find_rrset_signer(struct ub_packed_rrset_key* rrset, uint8_t** sname,
1653b6c3722Schristos 	size_t* slen)
1663b6c3722Schristos {
1673b6c3722Schristos 	struct packed_rrset_data* d = (struct packed_rrset_data*)
1683b6c3722Schristos 		rrset->entry.data;
1693b6c3722Schristos 	/* return signer for first signature, or NULL */
1703b6c3722Schristos 	if(d->rrsig_count == 0) {
1713b6c3722Schristos 		*sname = NULL;
1723b6c3722Schristos 		*slen = 0;
1733b6c3722Schristos 		return;
1743b6c3722Schristos 	}
1753b6c3722Schristos 	/* get rrsig signer name out of the signature */
1763b6c3722Schristos 	rrsig_get_signer(d->rr_data[d->count], d->rr_len[d->count],
1773b6c3722Schristos 		sname, slen);
1783b6c3722Schristos }
1793b6c3722Schristos 
1803b6c3722Schristos /**
1813b6c3722Schristos  * Find best signer name in this set of rrsigs.
1823b6c3722Schristos  * @param rrset: which rrsigs to look through.
1833b6c3722Schristos  * @param qinf: the query name that needs validation.
1843b6c3722Schristos  * @param signer_name: the best signer_name. Updated if a better one is found.
1853b6c3722Schristos  * @param signer_len: length of signer name.
1863b6c3722Schristos  * @param matchcount: count of current best name (starts at 0 for no match).
1873b6c3722Schristos  * 	Updated if match is improved.
1883b6c3722Schristos  */
1893b6c3722Schristos static void
val_find_best_signer(struct ub_packed_rrset_key * rrset,struct query_info * qinf,uint8_t ** signer_name,size_t * signer_len,int * matchcount)1903b6c3722Schristos val_find_best_signer(struct ub_packed_rrset_key* rrset,
1913b6c3722Schristos 	struct query_info* qinf, uint8_t** signer_name, size_t* signer_len,
1923b6c3722Schristos 	int* matchcount)
1933b6c3722Schristos {
1943b6c3722Schristos 	struct packed_rrset_data* d = (struct packed_rrset_data*)
1953b6c3722Schristos 		rrset->entry.data;
1963b6c3722Schristos 	uint8_t* sign;
1973b6c3722Schristos 	size_t i;
1983b6c3722Schristos 	int m;
1993b6c3722Schristos 	for(i=d->count; i<d->count+d->rrsig_count; i++) {
2003b6c3722Schristos 		sign = d->rr_data[i]+2+18;
2013b6c3722Schristos 		/* look at signatures that are valid (long enough),
2023b6c3722Schristos 		 * and have a signer name that is a superdomain of qname,
2033b6c3722Schristos 		 * and then check the number of labels in the shared topdomain
2043b6c3722Schristos 		 * improve the match if possible */
2053b6c3722Schristos 		if(d->rr_len[i] > 2+19 && /* rdata, sig + root label*/
2063b6c3722Schristos 			dname_subdomain_c(qinf->qname, sign)) {
2073b6c3722Schristos 			(void)dname_lab_cmp(qinf->qname,
2083b6c3722Schristos 				dname_count_labels(qinf->qname),
2093b6c3722Schristos 				sign, dname_count_labels(sign), &m);
2103b6c3722Schristos 			if(m > *matchcount) {
2113b6c3722Schristos 				*matchcount = m;
2123b6c3722Schristos 				*signer_name = sign;
2133b6c3722Schristos 				(void)dname_count_size_labels(*signer_name,
2143b6c3722Schristos 					signer_len);
2153b6c3722Schristos 			}
2163b6c3722Schristos 		}
2173b6c3722Schristos 	}
2183b6c3722Schristos }
2193b6c3722Schristos 
2203b6c3722Schristos void
val_find_signer(enum val_classification subtype,struct query_info * qinf,struct reply_info * rep,size_t skip,uint8_t ** signer_name,size_t * signer_len)2213b6c3722Schristos val_find_signer(enum val_classification subtype, struct query_info* qinf,
2223b6c3722Schristos 	struct reply_info* rep, size_t skip, uint8_t** signer_name,
2233b6c3722Schristos 	size_t* signer_len)
2243b6c3722Schristos {
2253b6c3722Schristos 	size_t i;
2263b6c3722Schristos 
2270cd9f4ecSchristos 	if(subtype == VAL_CLASS_POSITIVE) {
2283b6c3722Schristos 		/* check for the answer rrset */
2293b6c3722Schristos 		for(i=skip; i<rep->an_numrrsets; i++) {
2303b6c3722Schristos 			if(query_dname_compare(qinf->qname,
2313b6c3722Schristos 				rep->rrsets[i]->rk.dname) == 0) {
2323b6c3722Schristos 				val_find_rrset_signer(rep->rrsets[i],
2333b6c3722Schristos 					signer_name, signer_len);
2343b6c3722Schristos 				return;
2353b6c3722Schristos 			}
2363b6c3722Schristos 		}
2373b6c3722Schristos 		*signer_name = NULL;
2383b6c3722Schristos 		*signer_len = 0;
2393b6c3722Schristos 	} else if(subtype == VAL_CLASS_CNAME) {
2403b6c3722Schristos 		/* check for the first signed cname/dname rrset */
2413b6c3722Schristos 		for(i=skip; i<rep->an_numrrsets; i++) {
2423b6c3722Schristos 			val_find_rrset_signer(rep->rrsets[i],
2433b6c3722Schristos 				signer_name, signer_len);
2443b6c3722Schristos 			if(*signer_name)
2453b6c3722Schristos 				return;
2463b6c3722Schristos 			if(ntohs(rep->rrsets[i]->rk.type) != LDNS_RR_TYPE_DNAME)
2473b6c3722Schristos 				break; /* only check CNAME after a DNAME */
2483b6c3722Schristos 		}
2493b6c3722Schristos 		*signer_name = NULL;
2503b6c3722Schristos 		*signer_len = 0;
2513b6c3722Schristos 	} else if(subtype == VAL_CLASS_NAMEERROR
2523b6c3722Schristos 		|| subtype == VAL_CLASS_NODATA) {
2533b6c3722Schristos 		/*Check to see if the AUTH section NSEC record(s) have rrsigs*/
2543b6c3722Schristos 		for(i=rep->an_numrrsets; i<
2553b6c3722Schristos 			rep->an_numrrsets+rep->ns_numrrsets; i++) {
2563b6c3722Schristos 			if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NSEC
2573b6c3722Schristos 				|| ntohs(rep->rrsets[i]->rk.type) ==
2583b6c3722Schristos 				LDNS_RR_TYPE_NSEC3) {
2593b6c3722Schristos 				val_find_rrset_signer(rep->rrsets[i],
2603b6c3722Schristos 					signer_name, signer_len);
2613b6c3722Schristos 				return;
2623b6c3722Schristos 			}
2633b6c3722Schristos 		}
2643b6c3722Schristos 	} else if(subtype == VAL_CLASS_CNAMENOANSWER) {
2653b6c3722Schristos 		/* find closest superdomain signer name in authority section
2663b6c3722Schristos 		 * NSEC and NSEC3s */
2673b6c3722Schristos 		int matchcount = 0;
2683b6c3722Schristos 		*signer_name = NULL;
2693b6c3722Schristos 		*signer_len = 0;
2703b6c3722Schristos 		for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->
2713b6c3722Schristos 			ns_numrrsets; i++) {
2723b6c3722Schristos 			if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NSEC
2733b6c3722Schristos 				|| ntohs(rep->rrsets[i]->rk.type) ==
2743b6c3722Schristos 				LDNS_RR_TYPE_NSEC3) {
2753b6c3722Schristos 				val_find_best_signer(rep->rrsets[i], qinf,
2763b6c3722Schristos 					signer_name, signer_len, &matchcount);
2773b6c3722Schristos 			}
2783b6c3722Schristos 		}
2790cd9f4ecSchristos 	} else if(subtype == VAL_CLASS_ANY) {
2800cd9f4ecSchristos 		/* check for one of the answer rrset that has signatures,
2810cd9f4ecSchristos 		 * or potentially a DNAME is in use with a different qname */
2820cd9f4ecSchristos 		for(i=skip; i<rep->an_numrrsets; i++) {
2830cd9f4ecSchristos 			if(query_dname_compare(qinf->qname,
2840cd9f4ecSchristos 				rep->rrsets[i]->rk.dname) == 0) {
2850cd9f4ecSchristos 				val_find_rrset_signer(rep->rrsets[i],
2860cd9f4ecSchristos 					signer_name, signer_len);
2870cd9f4ecSchristos 				if(*signer_name)
2880cd9f4ecSchristos 					return;
2890cd9f4ecSchristos 			}
2900cd9f4ecSchristos 		}
2910cd9f4ecSchristos 		/* no answer RRSIGs with qname, try a DNAME */
2920cd9f4ecSchristos 		if(skip < rep->an_numrrsets &&
2930cd9f4ecSchristos 			ntohs(rep->rrsets[skip]->rk.type) ==
2940cd9f4ecSchristos 			LDNS_RR_TYPE_DNAME) {
2950cd9f4ecSchristos 			val_find_rrset_signer(rep->rrsets[skip],
2960cd9f4ecSchristos 				signer_name, signer_len);
2970cd9f4ecSchristos 			if(*signer_name)
2980cd9f4ecSchristos 				return;
2990cd9f4ecSchristos 		}
3000cd9f4ecSchristos 		*signer_name = NULL;
3010cd9f4ecSchristos 		*signer_len = 0;
3023b6c3722Schristos 	} else if(subtype == VAL_CLASS_REFERRAL) {
3033b6c3722Schristos 		/* find keys for the item at skip */
3043b6c3722Schristos 		if(skip < rep->rrset_count) {
3053b6c3722Schristos 			val_find_rrset_signer(rep->rrsets[skip],
3063b6c3722Schristos 				signer_name, signer_len);
3073b6c3722Schristos 			return;
3083b6c3722Schristos 		}
3093b6c3722Schristos 		*signer_name = NULL;
3103b6c3722Schristos 		*signer_len = 0;
3113b6c3722Schristos 	} else {
3123b6c3722Schristos 		verbose(VERB_QUERY, "find_signer: could not find signer name"
3133b6c3722Schristos 			" for unknown type response");
3143b6c3722Schristos 		*signer_name = NULL;
3153b6c3722Schristos 		*signer_len = 0;
3163b6c3722Schristos 	}
3173b6c3722Schristos }
3183b6c3722Schristos 
3193b6c3722Schristos /** return number of rrs in an rrset */
3203b6c3722Schristos static size_t
rrset_get_count(struct ub_packed_rrset_key * rrset)3213b6c3722Schristos rrset_get_count(struct ub_packed_rrset_key* rrset)
3223b6c3722Schristos {
3233b6c3722Schristos 	struct packed_rrset_data* d = (struct packed_rrset_data*)
3243b6c3722Schristos 		rrset->entry.data;
3253b6c3722Schristos 	if(!d) return 0;
3263b6c3722Schristos 	return d->count;
3273b6c3722Schristos }
3283b6c3722Schristos 
3293b6c3722Schristos /** return TTL of rrset */
3303b6c3722Schristos static uint32_t
rrset_get_ttl(struct ub_packed_rrset_key * rrset)3313b6c3722Schristos rrset_get_ttl(struct ub_packed_rrset_key* rrset)
3323b6c3722Schristos {
3333b6c3722Schristos 	struct packed_rrset_data* d = (struct packed_rrset_data*)
3343b6c3722Schristos 		rrset->entry.data;
3353b6c3722Schristos 	if(!d) return 0;
3363b6c3722Schristos 	return d->ttl;
3373b6c3722Schristos }
3383b6c3722Schristos 
3397a540f2bSchristos static enum sec_status
val_verify_rrset(struct module_env * env,struct val_env * ve,struct ub_packed_rrset_key * rrset,struct ub_packed_rrset_key * keys,uint8_t * sigalg,char ** reason,sldns_ede_code * reason_bogus,sldns_pkt_section section,struct module_qstate * qstate,int * verified)3403b6c3722Schristos val_verify_rrset(struct module_env* env, struct val_env* ve,
3413b6c3722Schristos         struct ub_packed_rrset_key* rrset, struct ub_packed_rrset_key* keys,
3427a540f2bSchristos 	uint8_t* sigalg, char** reason, sldns_ede_code *reason_bogus,
343*91f7d55fSchristos 	sldns_pkt_section section, struct module_qstate* qstate,
344*91f7d55fSchristos 	int *verified)
3453b6c3722Schristos {
3463b6c3722Schristos 	enum sec_status sec;
3473b6c3722Schristos 	struct packed_rrset_data* d = (struct packed_rrset_data*)rrset->
3483b6c3722Schristos 		entry.data;
3493b6c3722Schristos 	if(d->security == sec_status_secure) {
3503b6c3722Schristos 		/* re-verify all other statuses, because keyset may change*/
3513b6c3722Schristos 		log_nametypeclass(VERB_ALGO, "verify rrset cached",
3523b6c3722Schristos 			rrset->rk.dname, ntohs(rrset->rk.type),
3533b6c3722Schristos 			ntohs(rrset->rk.rrset_class));
354*91f7d55fSchristos 		*verified = 0;
3553b6c3722Schristos 		return d->security;
3563b6c3722Schristos 	}
3573b6c3722Schristos 	/* check in the cache if verification has already been done */
3583b6c3722Schristos 	rrset_check_sec_status(env->rrset_cache, rrset, *env->now);
3593b6c3722Schristos 	if(d->security == sec_status_secure) {
3603b6c3722Schristos 		log_nametypeclass(VERB_ALGO, "verify rrset from cache",
3613b6c3722Schristos 			rrset->rk.dname, ntohs(rrset->rk.type),
3623b6c3722Schristos 			ntohs(rrset->rk.rrset_class));
363*91f7d55fSchristos 		*verified = 0;
3643b6c3722Schristos 		return d->security;
3653b6c3722Schristos 	}
3663b6c3722Schristos 	log_nametypeclass(VERB_ALGO, "verify rrset", rrset->rk.dname,
3673b6c3722Schristos 		ntohs(rrset->rk.type), ntohs(rrset->rk.rrset_class));
3680cd9f4ecSchristos 	sec = dnskeyset_verify_rrset(env, ve, rrset, keys, sigalg, reason,
369*91f7d55fSchristos 		reason_bogus, section, qstate, verified);
3703b6c3722Schristos 	verbose(VERB_ALGO, "verify result: %s", sec_status_to_string(sec));
3713b6c3722Schristos 	regional_free_all(env->scratch);
3723b6c3722Schristos 
3733b6c3722Schristos 	/* update rrset security status
3743b6c3722Schristos 	 * only improves security status
3753b6c3722Schristos 	 * and bogus is set only once, even if we rechecked the status */
3763b6c3722Schristos 	if(sec > d->security) {
3773b6c3722Schristos 		d->security = sec;
3783b6c3722Schristos 		if(sec == sec_status_secure)
3793b6c3722Schristos 			d->trust = rrset_trust_validated;
3803b6c3722Schristos 		else if(sec == sec_status_bogus) {
3813b6c3722Schristos 			size_t i;
3823b6c3722Schristos 			/* update ttl for rrset to fixed value. */
3833b6c3722Schristos 			d->ttl = ve->bogus_ttl;
3843b6c3722Schristos 			for(i=0; i<d->count+d->rrsig_count; i++)
3853b6c3722Schristos 				d->rr_ttl[i] = ve->bogus_ttl;
3863b6c3722Schristos 			/* leave RR specific TTL: not used for determine
3873b6c3722Schristos 			 * if RRset timed out and clients see proper value. */
3883b6c3722Schristos 			lock_basic_lock(&ve->bogus_lock);
3893b6c3722Schristos 			ve->num_rrset_bogus++;
3903b6c3722Schristos 			lock_basic_unlock(&ve->bogus_lock);
3913b6c3722Schristos 		}
3923b6c3722Schristos 		/* if status updated - store in cache for reuse */
3933b6c3722Schristos 		rrset_update_sec_status(env->rrset_cache, rrset, *env->now);
3943b6c3722Schristos 	}
3953b6c3722Schristos 
3963b6c3722Schristos 	return sec;
3973b6c3722Schristos }
3983b6c3722Schristos 
3993b6c3722Schristos enum sec_status
val_verify_rrset_entry(struct module_env * env,struct val_env * ve,struct ub_packed_rrset_key * rrset,struct key_entry_key * kkey,char ** reason,sldns_ede_code * reason_bogus,sldns_pkt_section section,struct module_qstate * qstate,int * verified)4003b6c3722Schristos val_verify_rrset_entry(struct module_env* env, struct val_env* ve,
4013b6c3722Schristos         struct ub_packed_rrset_key* rrset, struct key_entry_key* kkey,
4027a540f2bSchristos 	char** reason, sldns_ede_code *reason_bogus,
403*91f7d55fSchristos 	sldns_pkt_section section, struct module_qstate* qstate,
404*91f7d55fSchristos 	int* verified)
4053b6c3722Schristos {
4063b6c3722Schristos 	/* temporary dnskey rrset-key */
4073b6c3722Schristos 	struct ub_packed_rrset_key dnskey;
4083b6c3722Schristos 	struct key_entry_data* kd = (struct key_entry_data*)kkey->entry.data;
4093b6c3722Schristos 	enum sec_status sec;
4103b6c3722Schristos 	dnskey.rk.type = htons(kd->rrset_type);
4113b6c3722Schristos 	dnskey.rk.rrset_class = htons(kkey->key_class);
4123b6c3722Schristos 	dnskey.rk.flags = 0;
4133b6c3722Schristos 	dnskey.rk.dname = kkey->name;
4143b6c3722Schristos 	dnskey.rk.dname_len = kkey->namelen;
4153b6c3722Schristos 	dnskey.entry.key = &dnskey;
4163b6c3722Schristos 	dnskey.entry.data = kd->rrset_data;
4170cd9f4ecSchristos 	sec = val_verify_rrset(env, ve, rrset, &dnskey, kd->algo, reason,
418*91f7d55fSchristos 		reason_bogus, section, qstate, verified);
4193b6c3722Schristos 	return sec;
4203b6c3722Schristos }
4213b6c3722Schristos 
4223b6c3722Schristos /** verify that a DS RR hashes to a key and that key signs the set */
4233b6c3722Schristos static enum sec_status
verify_dnskeys_with_ds_rr(struct module_env * env,struct val_env * ve,struct ub_packed_rrset_key * dnskey_rrset,struct ub_packed_rrset_key * ds_rrset,size_t ds_idx,char ** reason,sldns_ede_code * reason_bogus,struct module_qstate * qstate)4243b6c3722Schristos verify_dnskeys_with_ds_rr(struct module_env* env, struct val_env* ve,
4253b6c3722Schristos 	struct ub_packed_rrset_key* dnskey_rrset,
4260cd9f4ecSchristos         struct ub_packed_rrset_key* ds_rrset, size_t ds_idx, char** reason,
4277a540f2bSchristos 	sldns_ede_code *reason_bogus, struct module_qstate* qstate)
4283b6c3722Schristos {
4293b6c3722Schristos 	enum sec_status sec = sec_status_bogus;
4307a540f2bSchristos 	size_t i, num, numchecked = 0, numhashok = 0, numsizesupp = 0;
4313b6c3722Schristos 	num = rrset_get_count(dnskey_rrset);
4323b6c3722Schristos 	for(i=0; i<num; i++) {
4333b6c3722Schristos 		/* Skip DNSKEYs that don't match the basic criteria. */
4343b6c3722Schristos 		if(ds_get_key_algo(ds_rrset, ds_idx)
4353b6c3722Schristos 		   != dnskey_get_algo(dnskey_rrset, i)
4363b6c3722Schristos 		   || dnskey_calc_keytag(dnskey_rrset, i)
4373b6c3722Schristos 		   != ds_get_keytag(ds_rrset, ds_idx)) {
4383b6c3722Schristos 			continue;
4393b6c3722Schristos 		}
4403b6c3722Schristos 		numchecked++;
4413b6c3722Schristos 		verbose(VERB_ALGO, "attempt DS match algo %d keytag %d",
4423b6c3722Schristos 			ds_get_key_algo(ds_rrset, ds_idx),
4433b6c3722Schristos 			ds_get_keytag(ds_rrset, ds_idx));
4443b6c3722Schristos 
4453b6c3722Schristos 		/* Convert the candidate DNSKEY into a hash using the
4463b6c3722Schristos 		 * same DS hash algorithm. */
4473b6c3722Schristos 		if(!ds_digest_match_dnskey(env, dnskey_rrset, i, ds_rrset,
4483b6c3722Schristos 			ds_idx)) {
4493b6c3722Schristos 			verbose(VERB_ALGO, "DS match attempt failed");
450*91f7d55fSchristos 			if(numchecked > numhashok + MAX_DS_MATCH_FAILURES) {
451*91f7d55fSchristos 				verbose(VERB_ALGO, "DS match attempt reached "
452*91f7d55fSchristos 					"MAX_DS_MATCH_FAILURES (%d); bogus",
453*91f7d55fSchristos 					MAX_DS_MATCH_FAILURES);
454*91f7d55fSchristos 				return sec_status_bogus;
455*91f7d55fSchristos 			}
4563b6c3722Schristos 			continue;
4573b6c3722Schristos 		}
4583b6c3722Schristos 		numhashok++;
4597a540f2bSchristos 		if(!dnskey_size_is_supported(dnskey_rrset, i)) {
4607a540f2bSchristos 			verbose(VERB_ALGO, "DS okay but that DNSKEY size is not supported");
4617a540f2bSchristos 			numsizesupp++;
4627a540f2bSchristos 			continue;
4637a540f2bSchristos 		}
4643b6c3722Schristos 		verbose(VERB_ALGO, "DS match digest ok, trying signature");
4653b6c3722Schristos 
4663b6c3722Schristos 		/* Otherwise, we have a match! Make sure that the DNSKEY
4673b6c3722Schristos 		 * verifies *with this key*  */
4687a540f2bSchristos 		sec = dnskey_verify_rrset(env, ve, dnskey_rrset, dnskey_rrset,
4697a540f2bSchristos 			i, reason, reason_bogus, LDNS_SECTION_ANSWER, qstate);
4703b6c3722Schristos 		if(sec == sec_status_secure) {
4713b6c3722Schristos 			return sec;
4723b6c3722Schristos 		}
4733b6c3722Schristos 		/* If it didn't validate with the DNSKEY, try the next one! */
4743b6c3722Schristos 	}
4757a540f2bSchristos 	if(numsizesupp != 0 || sec == sec_status_indeterminate) {
4767a540f2bSchristos 		/* there is a working DS, but that DNSKEY is not supported */
4777a540f2bSchristos 		return sec_status_insecure;
4787a540f2bSchristos 	}
4793b6c3722Schristos 	if(numchecked == 0)
4803b6c3722Schristos 		algo_needs_reason(env, ds_get_key_algo(ds_rrset, ds_idx),
4813b6c3722Schristos 			reason, "no keys have a DS");
4823b6c3722Schristos 	else if(numhashok == 0)
4833b6c3722Schristos 		*reason = "DS hash mismatches key";
4843b6c3722Schristos 	else if(!*reason)
4853b6c3722Schristos 		*reason = "keyset not secured by DNSKEY that matches DS";
4863b6c3722Schristos 	return sec_status_bogus;
4873b6c3722Schristos }
4883b6c3722Schristos 
val_favorite_ds_algo(struct ub_packed_rrset_key * ds_rrset)4893b6c3722Schristos int val_favorite_ds_algo(struct ub_packed_rrset_key* ds_rrset)
4903b6c3722Schristos {
4913b6c3722Schristos 	size_t i, num = rrset_get_count(ds_rrset);
4923b6c3722Schristos 	int d, digest_algo = 0; /* DS digest algo 0 is not used. */
4933b6c3722Schristos 	/* find favorite algo, for now, highest number supported */
4943b6c3722Schristos 	for(i=0; i<num; i++) {
4953b6c3722Schristos 		if(!ds_digest_algo_is_supported(ds_rrset, i) ||
4963b6c3722Schristos 			!ds_key_algo_is_supported(ds_rrset, i)) {
4973b6c3722Schristos 			continue;
4983b6c3722Schristos 		}
4993b6c3722Schristos 		d = ds_get_digest_algo(ds_rrset, i);
5003b6c3722Schristos 		if(d > digest_algo)
5013b6c3722Schristos 			digest_algo = d;
5023b6c3722Schristos 	}
5033b6c3722Schristos 	return digest_algo;
5043b6c3722Schristos }
5053b6c3722Schristos 
5063b6c3722Schristos enum sec_status
val_verify_DNSKEY_with_DS(struct module_env * env,struct val_env * ve,struct ub_packed_rrset_key * dnskey_rrset,struct ub_packed_rrset_key * ds_rrset,uint8_t * sigalg,char ** reason,sldns_ede_code * reason_bogus,struct module_qstate * qstate)5073b6c3722Schristos val_verify_DNSKEY_with_DS(struct module_env* env, struct val_env* ve,
5083b6c3722Schristos 	struct ub_packed_rrset_key* dnskey_rrset,
5090cd9f4ecSchristos 	struct ub_packed_rrset_key* ds_rrset, uint8_t* sigalg, char** reason,
5107a540f2bSchristos 	sldns_ede_code *reason_bogus, struct module_qstate* qstate)
5113b6c3722Schristos {
5123b6c3722Schristos 	/* as long as this is false, we can consider this DS rrset to be
5133b6c3722Schristos 	 * equivalent to no DS rrset. */
5143b6c3722Schristos 	int has_useful_ds = 0, digest_algo, alg;
5153b6c3722Schristos 	struct algo_needs needs;
5163b6c3722Schristos 	size_t i, num;
5173b6c3722Schristos 	enum sec_status sec;
5183b6c3722Schristos 
5193b6c3722Schristos 	if(dnskey_rrset->rk.dname_len != ds_rrset->rk.dname_len ||
5203b6c3722Schristos 		query_dname_compare(dnskey_rrset->rk.dname, ds_rrset->rk.dname)
5213b6c3722Schristos 		!= 0) {
5223b6c3722Schristos 		verbose(VERB_QUERY, "DNSKEY RRset did not match DS RRset "
5233b6c3722Schristos 			"by name");
5243b6c3722Schristos 		*reason = "DNSKEY RRset did not match DS RRset by name";
5253b6c3722Schristos 		return sec_status_bogus;
5263b6c3722Schristos 	}
5273b6c3722Schristos 
5280cd9f4ecSchristos 	if(sigalg) {
5290cd9f4ecSchristos 		/* harden against algo downgrade is enabled */
5303b6c3722Schristos 		digest_algo = val_favorite_ds_algo(ds_rrset);
5313b6c3722Schristos 		algo_needs_init_ds(&needs, ds_rrset, digest_algo, sigalg);
5320cd9f4ecSchristos 	} else {
5330cd9f4ecSchristos 		/* accept any key algo, any digest algo */
5340cd9f4ecSchristos 		digest_algo = -1;
5350cd9f4ecSchristos 	}
5363b6c3722Schristos 	num = rrset_get_count(ds_rrset);
5373b6c3722Schristos 	for(i=0; i<num; i++) {
5383b6c3722Schristos 		/* Check to see if we can understand this DS.
5393b6c3722Schristos 		 * And check it is the strongest digest */
5403b6c3722Schristos 		if(!ds_digest_algo_is_supported(ds_rrset, i) ||
5413b6c3722Schristos 			!ds_key_algo_is_supported(ds_rrset, i) ||
5420cd9f4ecSchristos 			(sigalg && (ds_get_digest_algo(ds_rrset, i) != digest_algo))) {
5433b6c3722Schristos 			continue;
5443b6c3722Schristos 		}
5453b6c3722Schristos 
5467a540f2bSchristos 		sec = verify_dnskeys_with_ds_rr(env, ve, dnskey_rrset,
5477a540f2bSchristos 			ds_rrset, i, reason, reason_bogus, qstate);
5487a540f2bSchristos 		if(sec == sec_status_insecure)
5497a540f2bSchristos 			continue;
5507a540f2bSchristos 
5513b6c3722Schristos 		/* Once we see a single DS with a known digestID and
5523b6c3722Schristos 		 * algorithm, we cannot return INSECURE (with a
5533b6c3722Schristos 		 * "null" KeyEntry). */
5543b6c3722Schristos 		has_useful_ds = 1;
5553b6c3722Schristos 
5563b6c3722Schristos 		if(sec == sec_status_secure) {
5573b6c3722Schristos 			if(!sigalg || algo_needs_set_secure(&needs,
5583b6c3722Schristos 				(uint8_t)ds_get_key_algo(ds_rrset, i))) {
5593b6c3722Schristos 				verbose(VERB_ALGO, "DS matched DNSKEY.");
5607a540f2bSchristos 				if(!dnskeyset_size_is_supported(dnskey_rrset)) {
5617a540f2bSchristos 					verbose(VERB_ALGO, "DS works, but dnskeyset contain keys that are unsupported, treat as insecure");
5627a540f2bSchristos 					return sec_status_insecure;
5637a540f2bSchristos 				}
5643b6c3722Schristos 				return sec_status_secure;
5653b6c3722Schristos 			}
5663b6c3722Schristos 		} else if(sigalg && sec == sec_status_bogus) {
5673b6c3722Schristos 			algo_needs_set_bogus(&needs,
5683b6c3722Schristos 				(uint8_t)ds_get_key_algo(ds_rrset, i));
5693b6c3722Schristos 		}
5703b6c3722Schristos 	}
5713b6c3722Schristos 
5723b6c3722Schristos 	/* None of the DS's worked out. */
5733b6c3722Schristos 
5743b6c3722Schristos 	/* If no DSs were understandable, then this is OK. */
5753b6c3722Schristos 	if(!has_useful_ds) {
5763b6c3722Schristos 		verbose(VERB_ALGO, "No usable DS records were found -- "
5773b6c3722Schristos 			"treating as insecure.");
5783b6c3722Schristos 		return sec_status_insecure;
5793b6c3722Schristos 	}
5803b6c3722Schristos 	/* If any were understandable, then it is bad. */
5813b6c3722Schristos 	verbose(VERB_QUERY, "Failed to match any usable DS to a DNSKEY.");
5823b6c3722Schristos 	if(sigalg && (alg=algo_needs_missing(&needs)) != 0) {
5833b6c3722Schristos 		algo_needs_reason(env, alg, reason, "missing verification of "
5843b6c3722Schristos 			"DNSKEY signature");
5853b6c3722Schristos 	}
5863b6c3722Schristos 	return sec_status_bogus;
5873b6c3722Schristos }
5883b6c3722Schristos 
5893b6c3722Schristos struct key_entry_key*
val_verify_new_DNSKEYs(struct regional * region,struct module_env * env,struct val_env * ve,struct ub_packed_rrset_key * dnskey_rrset,struct ub_packed_rrset_key * ds_rrset,int downprot,char ** reason,sldns_ede_code * reason_bogus,struct module_qstate * qstate)5903b6c3722Schristos val_verify_new_DNSKEYs(struct regional* region, struct module_env* env,
5913b6c3722Schristos 	struct val_env* ve, struct ub_packed_rrset_key* dnskey_rrset,
5920cd9f4ecSchristos 	struct ub_packed_rrset_key* ds_rrset, int downprot, char** reason,
5937a540f2bSchristos 	sldns_ede_code *reason_bogus, struct module_qstate* qstate)
5943b6c3722Schristos {
5953b6c3722Schristos 	uint8_t sigalg[ALGO_NEEDS_MAX+1];
5963b6c3722Schristos 	enum sec_status sec = val_verify_DNSKEY_with_DS(env, ve,
5977a540f2bSchristos 		dnskey_rrset, ds_rrset, downprot?sigalg:NULL, reason,
5987a540f2bSchristos 		reason_bogus, qstate);
5993b6c3722Schristos 
6003b6c3722Schristos 	if(sec == sec_status_secure) {
6013b6c3722Schristos 		return key_entry_create_rrset(region,
6023b6c3722Schristos 			ds_rrset->rk.dname, ds_rrset->rk.dname_len,
6033b6c3722Schristos 			ntohs(ds_rrset->rk.rrset_class), dnskey_rrset,
604*91f7d55fSchristos 			downprot?sigalg:NULL, LDNS_EDE_NONE, NULL,
605*91f7d55fSchristos 			*env->now);
6063b6c3722Schristos 	} else if(sec == sec_status_insecure) {
6073b6c3722Schristos 		return key_entry_create_null(region, ds_rrset->rk.dname,
6083b6c3722Schristos 			ds_rrset->rk.dname_len,
6093b6c3722Schristos 			ntohs(ds_rrset->rk.rrset_class),
610*91f7d55fSchristos 			rrset_get_ttl(ds_rrset), *reason_bogus, *reason,
611*91f7d55fSchristos 			*env->now);
6123b6c3722Schristos 	}
6133b6c3722Schristos 	return key_entry_create_bad(region, ds_rrset->rk.dname,
6143b6c3722Schristos 		ds_rrset->rk.dname_len, ntohs(ds_rrset->rk.rrset_class),
615*91f7d55fSchristos 		BOGUS_KEY_TTL, *reason_bogus, *reason, *env->now);
6163b6c3722Schristos }
6173b6c3722Schristos 
6183b6c3722Schristos enum sec_status
val_verify_DNSKEY_with_TA(struct module_env * env,struct val_env * ve,struct ub_packed_rrset_key * dnskey_rrset,struct ub_packed_rrset_key * ta_ds,struct ub_packed_rrset_key * ta_dnskey,uint8_t * sigalg,char ** reason,sldns_ede_code * reason_bogus,struct module_qstate * qstate)6193b6c3722Schristos val_verify_DNSKEY_with_TA(struct module_env* env, struct val_env* ve,
6203b6c3722Schristos 	struct ub_packed_rrset_key* dnskey_rrset,
6213b6c3722Schristos 	struct ub_packed_rrset_key* ta_ds,
6220cd9f4ecSchristos 	struct ub_packed_rrset_key* ta_dnskey, uint8_t* sigalg, char** reason,
6237a540f2bSchristos 	sldns_ede_code *reason_bogus, struct module_qstate* qstate)
6243b6c3722Schristos {
6253b6c3722Schristos 	/* as long as this is false, we can consider this anchor to be
6263b6c3722Schristos 	 * equivalent to no anchor. */
6273b6c3722Schristos 	int has_useful_ta = 0, digest_algo = 0, alg;
6283b6c3722Schristos 	struct algo_needs needs;
6293b6c3722Schristos 	size_t i, num;
6303b6c3722Schristos 	enum sec_status sec;
6313b6c3722Schristos 
6323b6c3722Schristos 	if(ta_ds && (dnskey_rrset->rk.dname_len != ta_ds->rk.dname_len ||
6333b6c3722Schristos 		query_dname_compare(dnskey_rrset->rk.dname, ta_ds->rk.dname)
6343b6c3722Schristos 		!= 0)) {
6353b6c3722Schristos 		verbose(VERB_QUERY, "DNSKEY RRset did not match DS RRset "
6363b6c3722Schristos 			"by name");
6373b6c3722Schristos 		*reason = "DNSKEY RRset did not match DS RRset by name";
6387a540f2bSchristos 		if(reason_bogus)
6397a540f2bSchristos 			*reason_bogus = LDNS_EDE_DNSKEY_MISSING;
6403b6c3722Schristos 		return sec_status_bogus;
6413b6c3722Schristos 	}
6423b6c3722Schristos 	if(ta_dnskey && (dnskey_rrset->rk.dname_len != ta_dnskey->rk.dname_len
6433b6c3722Schristos 	     || query_dname_compare(dnskey_rrset->rk.dname, ta_dnskey->rk.dname)
6443b6c3722Schristos 		!= 0)) {
6453b6c3722Schristos 		verbose(VERB_QUERY, "DNSKEY RRset did not match anchor RRset "
6463b6c3722Schristos 			"by name");
6473b6c3722Schristos 		*reason = "DNSKEY RRset did not match anchor RRset by name";
6487a540f2bSchristos 		if(reason_bogus)
6497a540f2bSchristos 			*reason_bogus = LDNS_EDE_DNSKEY_MISSING;
6503b6c3722Schristos 		return sec_status_bogus;
6513b6c3722Schristos 	}
6523b6c3722Schristos 
6533b6c3722Schristos 	if(ta_ds)
6543b6c3722Schristos 		digest_algo = val_favorite_ds_algo(ta_ds);
6553b6c3722Schristos 	if(sigalg) {
6563b6c3722Schristos 		if(ta_ds)
6573b6c3722Schristos 			algo_needs_init_ds(&needs, ta_ds, digest_algo, sigalg);
6583b6c3722Schristos 		else	memset(&needs, 0, sizeof(needs));
6593b6c3722Schristos 		if(ta_dnskey)
6603b6c3722Schristos 			algo_needs_init_dnskey_add(&needs, ta_dnskey, sigalg);
6613b6c3722Schristos 	}
6623b6c3722Schristos 	if(ta_ds) {
6633b6c3722Schristos 	    num = rrset_get_count(ta_ds);
6643b6c3722Schristos 	    for(i=0; i<num; i++) {
6653b6c3722Schristos 		/* Check to see if we can understand this DS.
6663b6c3722Schristos 		 * And check it is the strongest digest */
6673b6c3722Schristos 		if(!ds_digest_algo_is_supported(ta_ds, i) ||
6683b6c3722Schristos 			!ds_key_algo_is_supported(ta_ds, i) ||
6693b6c3722Schristos 			ds_get_digest_algo(ta_ds, i) != digest_algo)
6703b6c3722Schristos 			continue;
6713b6c3722Schristos 
6727a540f2bSchristos 		sec = verify_dnskeys_with_ds_rr(env, ve, dnskey_rrset,
6737a540f2bSchristos 			ta_ds, i, reason, reason_bogus, qstate);
6747a540f2bSchristos 		if(sec == sec_status_insecure)
6757a540f2bSchristos 			continue;
6767a540f2bSchristos 
6773b6c3722Schristos 		/* Once we see a single DS with a known digestID and
6783b6c3722Schristos 		 * algorithm, we cannot return INSECURE (with a
6793b6c3722Schristos 		 * "null" KeyEntry). */
6803b6c3722Schristos 		has_useful_ta = 1;
6813b6c3722Schristos 
6823b6c3722Schristos 		if(sec == sec_status_secure) {
6833b6c3722Schristos 			if(!sigalg || algo_needs_set_secure(&needs,
6843b6c3722Schristos 				(uint8_t)ds_get_key_algo(ta_ds, i))) {
6853b6c3722Schristos 				verbose(VERB_ALGO, "DS matched DNSKEY.");
6867a540f2bSchristos 				if(!dnskeyset_size_is_supported(dnskey_rrset)) {
6877a540f2bSchristos 					verbose(VERB_ALGO, "trustanchor works, but dnskeyset contain keys that are unsupported, treat as insecure");
6887a540f2bSchristos 					return sec_status_insecure;
6897a540f2bSchristos 				}
6903b6c3722Schristos 				return sec_status_secure;
6913b6c3722Schristos 			}
6923b6c3722Schristos 		} else if(sigalg && sec == sec_status_bogus) {
6933b6c3722Schristos 			algo_needs_set_bogus(&needs,
6943b6c3722Schristos 				(uint8_t)ds_get_key_algo(ta_ds, i));
6953b6c3722Schristos 		}
6963b6c3722Schristos 	    }
6973b6c3722Schristos 	}
6983b6c3722Schristos 
6993b6c3722Schristos 	/* None of the DS's worked out: check the DNSKEYs. */
7003b6c3722Schristos 	if(ta_dnskey) {
7013b6c3722Schristos 	    num = rrset_get_count(ta_dnskey);
7023b6c3722Schristos 	    for(i=0; i<num; i++) {
7033b6c3722Schristos 		/* Check to see if we can understand this DNSKEY */
7043b6c3722Schristos 		if(!dnskey_algo_is_supported(ta_dnskey, i))
7053b6c3722Schristos 			continue;
7067a540f2bSchristos 		if(!dnskey_size_is_supported(ta_dnskey, i))
7077a540f2bSchristos 			continue;
7083b6c3722Schristos 
7093b6c3722Schristos 		/* we saw a useful TA */
7103b6c3722Schristos 		has_useful_ta = 1;
7113b6c3722Schristos 
7123b6c3722Schristos 		sec = dnskey_verify_rrset(env, ve, dnskey_rrset,
713*91f7d55fSchristos 			ta_dnskey, i, reason, reason_bogus, LDNS_SECTION_ANSWER, qstate);
7143b6c3722Schristos 		if(sec == sec_status_secure) {
7153b6c3722Schristos 			if(!sigalg || algo_needs_set_secure(&needs,
7163b6c3722Schristos 				(uint8_t)dnskey_get_algo(ta_dnskey, i))) {
7173b6c3722Schristos 				verbose(VERB_ALGO, "anchor matched DNSKEY.");
7187a540f2bSchristos 				if(!dnskeyset_size_is_supported(dnskey_rrset)) {
7197a540f2bSchristos 					verbose(VERB_ALGO, "trustanchor works, but dnskeyset contain keys that are unsupported, treat as insecure");
7207a540f2bSchristos 					return sec_status_insecure;
7217a540f2bSchristos 				}
7223b6c3722Schristos 				return sec_status_secure;
7233b6c3722Schristos 			}
7243b6c3722Schristos 		} else if(sigalg && sec == sec_status_bogus) {
7253b6c3722Schristos 			algo_needs_set_bogus(&needs,
7263b6c3722Schristos 				(uint8_t)dnskey_get_algo(ta_dnskey, i));
7273b6c3722Schristos 		}
7283b6c3722Schristos 	    }
7293b6c3722Schristos 	}
7303b6c3722Schristos 
7313b6c3722Schristos 	/* If no DSs were understandable, then this is OK. */
7323b6c3722Schristos 	if(!has_useful_ta) {
7333b6c3722Schristos 		verbose(VERB_ALGO, "No usable trust anchors were found -- "
7343b6c3722Schristos 			"treating as insecure.");
7353b6c3722Schristos 		return sec_status_insecure;
7363b6c3722Schristos 	}
7373b6c3722Schristos 	/* If any were understandable, then it is bad. */
7383b6c3722Schristos 	verbose(VERB_QUERY, "Failed to match any usable anchor to a DNSKEY.");
7393b6c3722Schristos 	if(sigalg && (alg=algo_needs_missing(&needs)) != 0) {
7403b6c3722Schristos 		algo_needs_reason(env, alg, reason, "missing verification of "
7413b6c3722Schristos 			"DNSKEY signature");
7423b6c3722Schristos 	}
7433b6c3722Schristos 	return sec_status_bogus;
7443b6c3722Schristos }
7453b6c3722Schristos 
7463b6c3722Schristos struct key_entry_key*
val_verify_new_DNSKEYs_with_ta(struct regional * region,struct module_env * env,struct val_env * ve,struct ub_packed_rrset_key * dnskey_rrset,struct ub_packed_rrset_key * ta_ds_rrset,struct ub_packed_rrset_key * ta_dnskey_rrset,int downprot,char ** reason,sldns_ede_code * reason_bogus,struct module_qstate * qstate)7473b6c3722Schristos val_verify_new_DNSKEYs_with_ta(struct regional* region, struct module_env* env,
7483b6c3722Schristos 	struct val_env* ve, struct ub_packed_rrset_key* dnskey_rrset,
7493b6c3722Schristos 	struct ub_packed_rrset_key* ta_ds_rrset,
7503b6c3722Schristos 	struct ub_packed_rrset_key* ta_dnskey_rrset, int downprot,
7517a540f2bSchristos 	char** reason, sldns_ede_code *reason_bogus, struct module_qstate* qstate)
7523b6c3722Schristos {
7533b6c3722Schristos 	uint8_t sigalg[ALGO_NEEDS_MAX+1];
7543b6c3722Schristos 	enum sec_status sec = val_verify_DNSKEY_with_TA(env, ve,
7553b6c3722Schristos 		dnskey_rrset, ta_ds_rrset, ta_dnskey_rrset,
7567a540f2bSchristos 		downprot?sigalg:NULL, reason, reason_bogus, qstate);
7573b6c3722Schristos 
7583b6c3722Schristos 	if(sec == sec_status_secure) {
7593b6c3722Schristos 		return key_entry_create_rrset(region,
7603b6c3722Schristos 			dnskey_rrset->rk.dname, dnskey_rrset->rk.dname_len,
7613b6c3722Schristos 			ntohs(dnskey_rrset->rk.rrset_class), dnskey_rrset,
762*91f7d55fSchristos 			downprot?sigalg:NULL, LDNS_EDE_NONE, NULL, *env->now);
7633b6c3722Schristos 	} else if(sec == sec_status_insecure) {
7643b6c3722Schristos 		return key_entry_create_null(region, dnskey_rrset->rk.dname,
7653b6c3722Schristos 			dnskey_rrset->rk.dname_len,
7663b6c3722Schristos 			ntohs(dnskey_rrset->rk.rrset_class),
767*91f7d55fSchristos 			rrset_get_ttl(dnskey_rrset), *reason_bogus, *reason,
768*91f7d55fSchristos 			*env->now);
7693b6c3722Schristos 	}
7703b6c3722Schristos 	return key_entry_create_bad(region, dnskey_rrset->rk.dname,
7713b6c3722Schristos 		dnskey_rrset->rk.dname_len, ntohs(dnskey_rrset->rk.rrset_class),
772*91f7d55fSchristos 		BOGUS_KEY_TTL, *reason_bogus, *reason, *env->now);
7733b6c3722Schristos }
7743b6c3722Schristos 
7753b6c3722Schristos int
val_dsset_isusable(struct ub_packed_rrset_key * ds_rrset)7763b6c3722Schristos val_dsset_isusable(struct ub_packed_rrset_key* ds_rrset)
7773b6c3722Schristos {
7783b6c3722Schristos 	size_t i;
7793b6c3722Schristos 	for(i=0; i<rrset_get_count(ds_rrset); i++) {
7803b6c3722Schristos 		if(ds_digest_algo_is_supported(ds_rrset, i) &&
7813b6c3722Schristos 			ds_key_algo_is_supported(ds_rrset, i))
7823b6c3722Schristos 			return 1;
7833b6c3722Schristos 	}
7843b6c3722Schristos 	if(verbosity < VERB_ALGO)
7853b6c3722Schristos 		return 0;
7863b6c3722Schristos 	if(rrset_get_count(ds_rrset) == 0)
7873b6c3722Schristos 		verbose(VERB_ALGO, "DS is not usable");
7883b6c3722Schristos 	else {
7893b6c3722Schristos 		/* report usability for the first DS RR */
7903b6c3722Schristos 		sldns_lookup_table *lt;
7913b6c3722Schristos 		char herr[64], aerr[64];
7923b6c3722Schristos 		lt = sldns_lookup_by_id(sldns_hashes,
7937a540f2bSchristos 			(int)ds_get_digest_algo(ds_rrset, 0));
7943b6c3722Schristos 		if(lt) snprintf(herr, sizeof(herr), "%s", lt->name);
7953b6c3722Schristos 		else snprintf(herr, sizeof(herr), "%d",
7967a540f2bSchristos 			(int)ds_get_digest_algo(ds_rrset, 0));
7973b6c3722Schristos 		lt = sldns_lookup_by_id(sldns_algorithms,
7987a540f2bSchristos 			(int)ds_get_key_algo(ds_rrset, 0));
7993b6c3722Schristos 		if(lt) snprintf(aerr, sizeof(aerr), "%s", lt->name);
8003b6c3722Schristos 		else snprintf(aerr, sizeof(aerr), "%d",
8017a540f2bSchristos 			(int)ds_get_key_algo(ds_rrset, 0));
8027a540f2bSchristos 
8033b6c3722Schristos 		verbose(VERB_ALGO, "DS unsupported, hash %s %s, "
8043b6c3722Schristos 			"key algorithm %s %s", herr,
8053b6c3722Schristos 			(ds_digest_algo_is_supported(ds_rrset, 0)?
8063b6c3722Schristos 			"(supported)":"(unsupported)"), aerr,
8073b6c3722Schristos 			(ds_key_algo_is_supported(ds_rrset, 0)?
8083b6c3722Schristos 			"(supported)":"(unsupported)"));
8093b6c3722Schristos 	}
8103b6c3722Schristos 	return 0;
8113b6c3722Schristos }
8123b6c3722Schristos 
8133b6c3722Schristos /** get label count for a signature */
8143b6c3722Schristos static uint8_t
rrsig_get_labcount(struct packed_rrset_data * d,size_t sig)8153b6c3722Schristos rrsig_get_labcount(struct packed_rrset_data* d, size_t sig)
8163b6c3722Schristos {
8173b6c3722Schristos 	if(d->rr_len[sig] < 2+4)
8183b6c3722Schristos 		return 0; /* bad sig length */
8193b6c3722Schristos 	return d->rr_data[sig][2+3];
8203b6c3722Schristos }
8213b6c3722Schristos 
8223b6c3722Schristos int
val_rrset_wildcard(struct ub_packed_rrset_key * rrset,uint8_t ** wc,size_t * wc_len)8237cd94d69Schristos val_rrset_wildcard(struct ub_packed_rrset_key* rrset, uint8_t** wc,
8247cd94d69Schristos 	size_t* wc_len)
8253b6c3722Schristos {
8263b6c3722Schristos 	struct packed_rrset_data* d = (struct packed_rrset_data*)rrset->
8273b6c3722Schristos 		entry.data;
8283b6c3722Schristos 	uint8_t labcount;
8293b6c3722Schristos 	int labdiff;
8303b6c3722Schristos 	uint8_t* wn;
8313b6c3722Schristos 	size_t i, wl;
8323b6c3722Schristos 	if(d->rrsig_count == 0) {
8333b6c3722Schristos 		return 1;
8343b6c3722Schristos 	}
8353b6c3722Schristos 	labcount = rrsig_get_labcount(d, d->count + 0);
8363b6c3722Schristos 	/* check rest of signatures identical */
8373b6c3722Schristos 	for(i=1; i<d->rrsig_count; i++) {
8383b6c3722Schristos 		if(labcount != rrsig_get_labcount(d, d->count + i)) {
8393b6c3722Schristos 			return 0;
8403b6c3722Schristos 		}
8413b6c3722Schristos 	}
8423b6c3722Schristos 	/* OK the rrsigs check out */
8433b6c3722Schristos 	/* if the RRSIG label count is shorter than the number of actual
8443b6c3722Schristos 	 * labels, then this rrset was synthesized from a wildcard.
8453b6c3722Schristos 	 * Note that the RRSIG label count doesn't count the root label. */
8463b6c3722Schristos 	wn = rrset->rk.dname;
8473b6c3722Schristos 	wl = rrset->rk.dname_len;
8483b6c3722Schristos 	/* skip a leading wildcard label in the dname (RFC4035 2.2) */
8493b6c3722Schristos 	if(dname_is_wild(wn)) {
8503b6c3722Schristos 		wn += 2;
8513b6c3722Schristos 		wl -= 2;
8523b6c3722Schristos 	}
8533b6c3722Schristos 	labdiff = (dname_count_labels(wn) - 1) - (int)labcount;
8543b6c3722Schristos 	if(labdiff > 0) {
8553b6c3722Schristos 		*wc = wn;
8563b6c3722Schristos 		dname_remove_labels(wc, &wl, labdiff);
8577cd94d69Schristos 		*wc_len = wl;
8583b6c3722Schristos 		return 1;
8593b6c3722Schristos 	}
8603b6c3722Schristos 	return 1;
8613b6c3722Schristos }
8623b6c3722Schristos 
8633b6c3722Schristos int
val_chase_cname(struct query_info * qchase,struct reply_info * rep,size_t * cname_skip)8643b6c3722Schristos val_chase_cname(struct query_info* qchase, struct reply_info* rep,
8653b6c3722Schristos 	size_t* cname_skip) {
8663b6c3722Schristos 	size_t i;
8673b6c3722Schristos 	/* skip any DNAMEs, go to the CNAME for next part */
8683b6c3722Schristos 	for(i = *cname_skip; i < rep->an_numrrsets; i++) {
8693b6c3722Schristos 		if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_CNAME &&
8703b6c3722Schristos 			query_dname_compare(qchase->qname, rep->rrsets[i]->
8713b6c3722Schristos 				rk.dname) == 0) {
8723b6c3722Schristos 			qchase->qname = NULL;
8733b6c3722Schristos 			get_cname_target(rep->rrsets[i], &qchase->qname,
8743b6c3722Schristos 				&qchase->qname_len);
8753b6c3722Schristos 			if(!qchase->qname)
8763b6c3722Schristos 				return 0; /* bad CNAME rdata */
8773b6c3722Schristos 			(*cname_skip) = i+1;
8783b6c3722Schristos 			return 1;
8793b6c3722Schristos 		}
8803b6c3722Schristos 	}
8813b6c3722Schristos 	return 0; /* CNAME classified but no matching CNAME ?! */
8823b6c3722Schristos }
8833b6c3722Schristos 
8843b6c3722Schristos /** see if rrset has signer name as one of the rrsig signers */
8853b6c3722Schristos static int
rrset_has_signer(struct ub_packed_rrset_key * rrset,uint8_t * name,size_t len)8863b6c3722Schristos rrset_has_signer(struct ub_packed_rrset_key* rrset, uint8_t* name, size_t len)
8873b6c3722Schristos {
8883b6c3722Schristos 	struct packed_rrset_data* d = (struct packed_rrset_data*)rrset->
8893b6c3722Schristos 		entry.data;
8903b6c3722Schristos 	size_t i;
8913b6c3722Schristos 	for(i = d->count; i< d->count+d->rrsig_count; i++) {
8923b6c3722Schristos 		if(d->rr_len[i] > 2+18+len) {
8933b6c3722Schristos 			/* at least rdatalen + signature + signame (+1 sig)*/
8943b6c3722Schristos 			if(!dname_valid(d->rr_data[i]+2+18, d->rr_len[i]-2-18))
8953b6c3722Schristos 				continue;
8963b6c3722Schristos 			if(query_dname_compare(name, d->rr_data[i]+2+18) == 0)
8973b6c3722Schristos 			{
8983b6c3722Schristos 				return 1;
8993b6c3722Schristos 			}
9003b6c3722Schristos 		}
9013b6c3722Schristos 	}
9023b6c3722Schristos 	return 0;
9033b6c3722Schristos }
9043b6c3722Schristos 
9053b6c3722Schristos void
val_fill_reply(struct reply_info * chase,struct reply_info * orig,size_t skip,uint8_t * name,size_t len,uint8_t * signer)9063b6c3722Schristos val_fill_reply(struct reply_info* chase, struct reply_info* orig,
9073b6c3722Schristos 	size_t skip, uint8_t* name, size_t len, uint8_t* signer)
9083b6c3722Schristos {
9093b6c3722Schristos 	size_t i;
9103b6c3722Schristos 	int seen_dname = 0;
9113b6c3722Schristos 	chase->rrset_count = 0;
9123b6c3722Schristos 	chase->an_numrrsets = 0;
9133b6c3722Schristos 	chase->ns_numrrsets = 0;
9143b6c3722Schristos 	chase->ar_numrrsets = 0;
9153b6c3722Schristos 	/* ANSWER section */
9163b6c3722Schristos 	for(i=skip; i<orig->an_numrrsets; i++) {
9173b6c3722Schristos 		if(!signer) {
9183b6c3722Schristos 			if(query_dname_compare(name,
9193b6c3722Schristos 				orig->rrsets[i]->rk.dname) == 0)
9203b6c3722Schristos 				chase->rrsets[chase->an_numrrsets++] =
9213b6c3722Schristos 					orig->rrsets[i];
9223b6c3722Schristos 		} else if(seen_dname && ntohs(orig->rrsets[i]->rk.type) ==
9233b6c3722Schristos 			LDNS_RR_TYPE_CNAME) {
9243b6c3722Schristos 			chase->rrsets[chase->an_numrrsets++] = orig->rrsets[i];
9253b6c3722Schristos 			seen_dname = 0;
9263b6c3722Schristos 		} else if(rrset_has_signer(orig->rrsets[i], name, len)) {
9273b6c3722Schristos 			chase->rrsets[chase->an_numrrsets++] = orig->rrsets[i];
9283b6c3722Schristos 			if(ntohs(orig->rrsets[i]->rk.type) ==
9293b6c3722Schristos 				LDNS_RR_TYPE_DNAME) {
9303b6c3722Schristos 					seen_dname = 1;
9313b6c3722Schristos 			}
9323b6c3722Schristos 		}
9333b6c3722Schristos 	}
9343b6c3722Schristos 	/* AUTHORITY section */
9353b6c3722Schristos 	for(i = (skip > orig->an_numrrsets)?skip:orig->an_numrrsets;
9363b6c3722Schristos 		i<orig->an_numrrsets+orig->ns_numrrsets;
9373b6c3722Schristos 		i++) {
9383b6c3722Schristos 		if(!signer) {
9393b6c3722Schristos 			if(query_dname_compare(name,
9403b6c3722Schristos 				orig->rrsets[i]->rk.dname) == 0)
9413b6c3722Schristos 				chase->rrsets[chase->an_numrrsets+
9423b6c3722Schristos 				    chase->ns_numrrsets++] = orig->rrsets[i];
9433b6c3722Schristos 		} else if(rrset_has_signer(orig->rrsets[i], name, len)) {
9443b6c3722Schristos 			chase->rrsets[chase->an_numrrsets+
9453b6c3722Schristos 				chase->ns_numrrsets++] = orig->rrsets[i];
9463b6c3722Schristos 		}
9473b6c3722Schristos 	}
9483b6c3722Schristos 	/* ADDITIONAL section */
9493b6c3722Schristos 	for(i= (skip>orig->an_numrrsets+orig->ns_numrrsets)?
9503b6c3722Schristos 		skip:orig->an_numrrsets+orig->ns_numrrsets;
9513b6c3722Schristos 		i<orig->rrset_count; i++) {
9523b6c3722Schristos 		if(!signer) {
9533b6c3722Schristos 			if(query_dname_compare(name,
9543b6c3722Schristos 				orig->rrsets[i]->rk.dname) == 0)
9553b6c3722Schristos 			    chase->rrsets[chase->an_numrrsets
9563b6c3722Schristos 				+orig->ns_numrrsets+chase->ar_numrrsets++]
9573b6c3722Schristos 				= orig->rrsets[i];
9583b6c3722Schristos 		} else if(rrset_has_signer(orig->rrsets[i], name, len)) {
9593b6c3722Schristos 			chase->rrsets[chase->an_numrrsets+orig->ns_numrrsets+
9603b6c3722Schristos 				chase->ar_numrrsets++] = orig->rrsets[i];
9613b6c3722Schristos 		}
9623b6c3722Schristos 	}
9633b6c3722Schristos 	chase->rrset_count = chase->an_numrrsets + chase->ns_numrrsets +
9643b6c3722Schristos 		chase->ar_numrrsets;
9653b6c3722Schristos }
9663b6c3722Schristos 
val_reply_remove_auth(struct reply_info * rep,size_t index)9673b6c3722Schristos void val_reply_remove_auth(struct reply_info* rep, size_t index)
9683b6c3722Schristos {
9693b6c3722Schristos 	log_assert(index < rep->rrset_count);
9703b6c3722Schristos 	log_assert(index >= rep->an_numrrsets);
9713b6c3722Schristos 	log_assert(index < rep->an_numrrsets+rep->ns_numrrsets);
9723b6c3722Schristos 	memmove(rep->rrsets+index, rep->rrsets+index+1,
9733b6c3722Schristos 		sizeof(struct ub_packed_rrset_key*)*
9743b6c3722Schristos 		(rep->rrset_count - index - 1));
9753b6c3722Schristos 	rep->ns_numrrsets--;
9763b6c3722Schristos 	rep->rrset_count--;
9773b6c3722Schristos }
9783b6c3722Schristos 
9793b6c3722Schristos void
val_check_nonsecure(struct module_env * env,struct reply_info * rep)9800cd9f4ecSchristos val_check_nonsecure(struct module_env* env, struct reply_info* rep)
9813b6c3722Schristos {
9823b6c3722Schristos 	size_t i;
9833b6c3722Schristos 	/* authority */
9843b6c3722Schristos 	for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
9853b6c3722Schristos 		if(((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
9863b6c3722Schristos 			->security != sec_status_secure) {
9873b6c3722Schristos 			/* because we want to return the authentic original
9883b6c3722Schristos 			 * message when presented with CD-flagged queries,
9893b6c3722Schristos 			 * we need to preserve AUTHORITY section data.
9903b6c3722Schristos 			 * However, this rrset is not signed or signed
9913b6c3722Schristos 			 * with the wrong keys. Validation has tried to
9923b6c3722Schristos 			 * verify this rrset with the keysets of import.
9933b6c3722Schristos 			 * But this rrset did not verify.
9943b6c3722Schristos 			 * Therefore the message is bogus.
9953b6c3722Schristos 			 */
9963b6c3722Schristos 
9970cd9f4ecSchristos 			/* check if authority has an NS record
9983b6c3722Schristos 			 * which is bad, and there is an answer section with
9993b6c3722Schristos 			 * data.  In that case, delete NS and additional to
10003b6c3722Schristos 			 * be lenient and make a minimal response */
10010cd9f4ecSchristos 			if(rep->an_numrrsets != 0 &&
10023b6c3722Schristos 				ntohs(rep->rrsets[i]->rk.type)
10033b6c3722Schristos 				== LDNS_RR_TYPE_NS) {
10043b6c3722Schristos 				verbose(VERB_ALGO, "truncate to minimal");
10053b6c3722Schristos 				rep->ar_numrrsets = 0;
10060cd9f4ecSchristos 				rep->rrset_count = rep->an_numrrsets +
10070cd9f4ecSchristos 					rep->ns_numrrsets;
10080cd9f4ecSchristos 				/* remove this unneeded authority rrset */
10090cd9f4ecSchristos 				memmove(rep->rrsets+i, rep->rrsets+i+1,
10100cd9f4ecSchristos 					sizeof(struct ub_packed_rrset_key*)*
10110cd9f4ecSchristos 					(rep->rrset_count - i - 1));
10120cd9f4ecSchristos 				rep->ns_numrrsets--;
10130cd9f4ecSchristos 				rep->rrset_count--;
10140cd9f4ecSchristos 				i--;
10153b6c3722Schristos 				return;
10163b6c3722Schristos 			}
10173b6c3722Schristos 
10183b6c3722Schristos 			log_nametypeclass(VERB_QUERY, "message is bogus, "
10193b6c3722Schristos 				"non secure rrset",
10203b6c3722Schristos 				rep->rrsets[i]->rk.dname,
10213b6c3722Schristos 				ntohs(rep->rrsets[i]->rk.type),
10223b6c3722Schristos 				ntohs(rep->rrsets[i]->rk.rrset_class));
10233b6c3722Schristos 			rep->security = sec_status_bogus;
10243b6c3722Schristos 			return;
10253b6c3722Schristos 		}
10263b6c3722Schristos 	}
10273b6c3722Schristos 	/* additional */
10280cd9f4ecSchristos 	if(!env->cfg->val_clean_additional)
10293b6c3722Schristos 		return;
10303b6c3722Schristos 	for(i=rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
10313b6c3722Schristos 		if(((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
10323b6c3722Schristos 			->security != sec_status_secure) {
10333b6c3722Schristos 			/* This does not cause message invalidation. It was
10343b6c3722Schristos 			 * simply unsigned data in the additional. The
10353b6c3722Schristos 			 * RRSIG must have been truncated off the message.
10363b6c3722Schristos 			 *
10373b6c3722Schristos 			 * However, we do not want to return possible bogus
10383b6c3722Schristos 			 * data to clients that rely on this service for
10393b6c3722Schristos 			 * their authentication.
10403b6c3722Schristos 			 */
10413b6c3722Schristos 			/* remove this unneeded additional rrset */
10423b6c3722Schristos 			memmove(rep->rrsets+i, rep->rrsets+i+1,
10433b6c3722Schristos 				sizeof(struct ub_packed_rrset_key*)*
10443b6c3722Schristos 				(rep->rrset_count - i - 1));
10453b6c3722Schristos 			rep->ar_numrrsets--;
10463b6c3722Schristos 			rep->rrset_count--;
10473b6c3722Schristos 			i--;
10483b6c3722Schristos 		}
10493b6c3722Schristos 	}
10503b6c3722Schristos }
10513b6c3722Schristos 
10523b6c3722Schristos /** check no anchor and unlock */
10533b6c3722Schristos static int
check_no_anchor(struct val_anchors * anchors,uint8_t * nm,size_t l,uint16_t c)10543b6c3722Schristos check_no_anchor(struct val_anchors* anchors, uint8_t* nm, size_t l, uint16_t c)
10553b6c3722Schristos {
10563b6c3722Schristos 	struct trust_anchor* ta;
10573b6c3722Schristos 	if((ta=anchors_lookup(anchors, nm, l, c))) {
10583b6c3722Schristos 		lock_basic_unlock(&ta->lock);
10593b6c3722Schristos 	}
10603b6c3722Schristos 	return !ta;
10613b6c3722Schristos }
10623b6c3722Schristos 
10633b6c3722Schristos void
val_mark_indeterminate(struct reply_info * rep,struct val_anchors * anchors,struct rrset_cache * r,struct module_env * env)10643b6c3722Schristos val_mark_indeterminate(struct reply_info* rep, struct val_anchors* anchors,
10653b6c3722Schristos 	struct rrset_cache* r, struct module_env* env)
10663b6c3722Schristos {
10673b6c3722Schristos 	size_t i;
10683b6c3722Schristos 	struct packed_rrset_data* d;
10693b6c3722Schristos 	for(i=0; i<rep->rrset_count; i++) {
10703b6c3722Schristos 		d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data;
10713b6c3722Schristos 		if(d->security == sec_status_unchecked &&
10723b6c3722Schristos 		   check_no_anchor(anchors, rep->rrsets[i]->rk.dname,
10733b6c3722Schristos 			rep->rrsets[i]->rk.dname_len,
10743b6c3722Schristos 			ntohs(rep->rrsets[i]->rk.rrset_class)))
10753b6c3722Schristos 		{
10763b6c3722Schristos 			/* mark as indeterminate */
10773b6c3722Schristos 			d->security = sec_status_indeterminate;
10783b6c3722Schristos 			rrset_update_sec_status(r, rep->rrsets[i], *env->now);
10793b6c3722Schristos 		}
10803b6c3722Schristos 	}
10813b6c3722Schristos }
10823b6c3722Schristos 
10833b6c3722Schristos void
val_mark_insecure(struct reply_info * rep,uint8_t * kname,struct rrset_cache * r,struct module_env * env)10843b6c3722Schristos val_mark_insecure(struct reply_info* rep, uint8_t* kname,
10853b6c3722Schristos 	struct rrset_cache* r, struct module_env* env)
10863b6c3722Schristos {
10873b6c3722Schristos 	size_t i;
10883b6c3722Schristos 	struct packed_rrset_data* d;
10893b6c3722Schristos 	for(i=0; i<rep->rrset_count; i++) {
10903b6c3722Schristos 		d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data;
10913b6c3722Schristos 		if(d->security == sec_status_unchecked &&
10923b6c3722Schristos 		   dname_subdomain_c(rep->rrsets[i]->rk.dname, kname)) {
10933b6c3722Schristos 			/* mark as insecure */
10943b6c3722Schristos 			d->security = sec_status_insecure;
10953b6c3722Schristos 			rrset_update_sec_status(r, rep->rrsets[i], *env->now);
10963b6c3722Schristos 		}
10973b6c3722Schristos 	}
10983b6c3722Schristos }
10993b6c3722Schristos 
11003b6c3722Schristos size_t
val_next_unchecked(struct reply_info * rep,size_t skip)11013b6c3722Schristos val_next_unchecked(struct reply_info* rep, size_t skip)
11023b6c3722Schristos {
11033b6c3722Schristos 	size_t i;
11043b6c3722Schristos 	struct packed_rrset_data* d;
11053b6c3722Schristos 	for(i=skip+1; i<rep->rrset_count; i++) {
11063b6c3722Schristos 		d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data;
11073b6c3722Schristos 		if(d->security == sec_status_unchecked) {
11083b6c3722Schristos 			return i;
11093b6c3722Schristos 		}
11103b6c3722Schristos 	}
11113b6c3722Schristos 	return rep->rrset_count;
11123b6c3722Schristos }
11133b6c3722Schristos 
11143b6c3722Schristos const char*
val_classification_to_string(enum val_classification subtype)11153b6c3722Schristos val_classification_to_string(enum val_classification subtype)
11163b6c3722Schristos {
11173b6c3722Schristos 	switch(subtype) {
11183b6c3722Schristos 		case VAL_CLASS_UNTYPED: 	return "untyped";
11193b6c3722Schristos 		case VAL_CLASS_UNKNOWN: 	return "unknown";
11203b6c3722Schristos 		case VAL_CLASS_POSITIVE: 	return "positive";
11213b6c3722Schristos 		case VAL_CLASS_CNAME: 		return "cname";
11223b6c3722Schristos 		case VAL_CLASS_NODATA: 		return "nodata";
11233b6c3722Schristos 		case VAL_CLASS_NAMEERROR: 	return "nameerror";
11243b6c3722Schristos 		case VAL_CLASS_CNAMENOANSWER: 	return "cnamenoanswer";
11253b6c3722Schristos 		case VAL_CLASS_REFERRAL: 	return "referral";
11263b6c3722Schristos 		case VAL_CLASS_ANY: 		return "qtype_any";
11273b6c3722Schristos 		default:
11283b6c3722Schristos 			return "bad_val_classification";
11293b6c3722Schristos 	}
11303b6c3722Schristos }
11313b6c3722Schristos 
11323b6c3722Schristos /** log a sock_list entry */
11333b6c3722Schristos static void
sock_list_logentry(enum verbosity_value v,const char * s,struct sock_list * p)11343b6c3722Schristos sock_list_logentry(enum verbosity_value v, const char* s, struct sock_list* p)
11353b6c3722Schristos {
11363b6c3722Schristos 	if(p->len)
11373b6c3722Schristos 		log_addr(v, s, &p->addr, p->len);
11383b6c3722Schristos 	else	verbose(v, "%s cache", s);
11393b6c3722Schristos }
11403b6c3722Schristos 
val_blacklist(struct sock_list ** blacklist,struct regional * region,struct sock_list * origin,int cross)11413b6c3722Schristos void val_blacklist(struct sock_list** blacklist, struct regional* region,
11423b6c3722Schristos 	struct sock_list* origin, int cross)
11433b6c3722Schristos {
11443b6c3722Schristos 	/* debug printout */
11453b6c3722Schristos 	if(verbosity >= VERB_ALGO) {
11463b6c3722Schristos 		struct sock_list* p;
11473b6c3722Schristos 		for(p=*blacklist; p; p=p->next)
11483b6c3722Schristos 			sock_list_logentry(VERB_ALGO, "blacklist", p);
11493b6c3722Schristos 		if(!origin)
11503b6c3722Schristos 			verbose(VERB_ALGO, "blacklist add: cache");
11513b6c3722Schristos 		for(p=origin; p; p=p->next)
11523b6c3722Schristos 			sock_list_logentry(VERB_ALGO, "blacklist add", p);
11533b6c3722Schristos 	}
11543b6c3722Schristos 	/* blacklist the IPs or the cache */
11553b6c3722Schristos 	if(!origin) {
11563b6c3722Schristos 		/* only add if nothing there. anything else also stops cache*/
11573b6c3722Schristos 		if(!*blacklist)
11583b6c3722Schristos 			sock_list_insert(blacklist, NULL, 0, region);
11593b6c3722Schristos 	} else if(!cross)
11603b6c3722Schristos 		sock_list_prepend(blacklist, origin);
11613b6c3722Schristos 	else	sock_list_merge(blacklist, region, origin);
11623b6c3722Schristos }
11633b6c3722Schristos 
val_has_signed_nsecs(struct reply_info * rep,char ** reason)11643b6c3722Schristos int val_has_signed_nsecs(struct reply_info* rep, char** reason)
11653b6c3722Schristos {
11663b6c3722Schristos 	size_t i, num_nsec = 0, num_nsec3 = 0;
11673b6c3722Schristos 	struct packed_rrset_data* d;
11683b6c3722Schristos 	for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
11693b6c3722Schristos 		if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NSEC))
11703b6c3722Schristos 			num_nsec++;
11713b6c3722Schristos 		else if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NSEC3))
11723b6c3722Schristos 			num_nsec3++;
11733b6c3722Schristos 		else continue;
11743b6c3722Schristos 		d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data;
11753b6c3722Schristos 		if(d && d->rrsig_count != 0) {
11763b6c3722Schristos 			return 1;
11773b6c3722Schristos 		}
11783b6c3722Schristos 	}
11793b6c3722Schristos 	if(num_nsec == 0 && num_nsec3 == 0)
11803b6c3722Schristos 		*reason = "no DNSSEC records";
11813b6c3722Schristos 	else if(num_nsec != 0)
11823b6c3722Schristos 		*reason = "no signatures over NSECs";
11833b6c3722Schristos 	else	*reason = "no signatures over NSEC3s";
11843b6c3722Schristos 	return 0;
11853b6c3722Schristos }
11863b6c3722Schristos 
11873b6c3722Schristos struct dns_msg*
val_find_DS(struct module_env * env,uint8_t * nm,size_t nmlen,uint16_t c,struct regional * region,uint8_t * topname)11883b6c3722Schristos val_find_DS(struct module_env* env, uint8_t* nm, size_t nmlen, uint16_t c,
11893b6c3722Schristos 	struct regional* region, uint8_t* topname)
11903b6c3722Schristos {
11913b6c3722Schristos 	struct dns_msg* msg;
11923b6c3722Schristos 	struct query_info qinfo;
11933b6c3722Schristos 	struct ub_packed_rrset_key *rrset = rrset_cache_lookup(
11943b6c3722Schristos 		env->rrset_cache, nm, nmlen, LDNS_RR_TYPE_DS, c, 0,
11953b6c3722Schristos 		*env->now, 0);
11963b6c3722Schristos 	if(rrset) {
11973b6c3722Schristos 		/* DS rrset exists. Return it to the validator immediately*/
11983b6c3722Schristos 		struct ub_packed_rrset_key* copy = packed_rrset_copy_region(
11993b6c3722Schristos 			rrset, region, *env->now);
12003b6c3722Schristos 		lock_rw_unlock(&rrset->entry.lock);
12013b6c3722Schristos 		if(!copy)
12023b6c3722Schristos 			return NULL;
12033b6c3722Schristos 		msg = dns_msg_create(nm, nmlen, LDNS_RR_TYPE_DS, c, region, 1);
12043b6c3722Schristos 		if(!msg)
12053b6c3722Schristos 			return NULL;
12063b6c3722Schristos 		msg->rep->rrsets[0] = copy;
12073b6c3722Schristos 		msg->rep->rrset_count++;
12083b6c3722Schristos 		msg->rep->an_numrrsets++;
12093b6c3722Schristos 		return msg;
12103b6c3722Schristos 	}
12113b6c3722Schristos 	/* lookup in rrset and negative cache for NSEC/NSEC3 */
12123b6c3722Schristos 	qinfo.qname = nm;
12133b6c3722Schristos 	qinfo.qname_len = nmlen;
12143b6c3722Schristos 	qinfo.qtype = LDNS_RR_TYPE_DS;
12153b6c3722Schristos 	qinfo.qclass = c;
12160cd9f4ecSchristos 	qinfo.local_alias = NULL;
12173b6c3722Schristos 	/* do not add SOA to reply message, it is going to be used internal */
12183b6c3722Schristos 	msg = val_neg_getmsg(env->neg_cache, &qinfo, region, env->rrset_cache,
12197cd94d69Schristos 		env->scratch_buffer, *env->now, 0, topname, env->cfg);
12203b6c3722Schristos 	return msg;
12213b6c3722Schristos }
1222