xref: /freebsd/contrib/unbound/validator/val_utils.c (revision 19261079b74319502c6ffa1249920079f0f69a72)
1 /*
2  * validator/val_utils.c - validator utility functions.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains helper functions for the validator module.
40  */
41 #include "config.h"
42 #include "validator/val_utils.h"
43 #include "validator/validator.h"
44 #include "validator/val_kentry.h"
45 #include "validator/val_sigcrypt.h"
46 #include "validator/val_anchor.h"
47 #include "validator/val_nsec.h"
48 #include "validator/val_neg.h"
49 #include "services/cache/rrset.h"
50 #include "services/cache/dns.h"
51 #include "util/data/msgreply.h"
52 #include "util/data/packed_rrset.h"
53 #include "util/data/dname.h"
54 #include "util/net_help.h"
55 #include "util/module.h"
56 #include "util/regional.h"
57 #include "util/config_file.h"
58 #include "sldns/wire2str.h"
59 #include "sldns/parseutil.h"
60 
61 enum val_classification
62 val_classify_response(uint16_t query_flags, struct query_info* origqinf,
63 	struct query_info* qinf, struct reply_info* rep, size_t skip)
64 {
65 	int rcode = (int)FLAGS_GET_RCODE(rep->flags);
66 	size_t i;
67 
68 	/* Normal Name Error's are easy to detect -- but don't mistake a CNAME
69 	 * chain ending in NXDOMAIN. */
70 	if(rcode == LDNS_RCODE_NXDOMAIN && rep->an_numrrsets == 0)
71 		return VAL_CLASS_NAMEERROR;
72 
73 	/* check for referral: nonRD query and it looks like a nodata */
74 	if(!(query_flags&BIT_RD) && rep->an_numrrsets == 0 &&
75 		rcode == LDNS_RCODE_NOERROR) {
76 		/* SOA record in auth indicates it is NODATA instead.
77 		 * All validation requiring NODATA messages have SOA in
78 		 * authority section. */
79 		/* uses fact that answer section is empty */
80 		int saw_ns = 0;
81 		for(i=0; i<rep->ns_numrrsets; i++) {
82 			if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_SOA)
83 				return VAL_CLASS_NODATA;
84 			if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_DS)
85 				return VAL_CLASS_REFERRAL;
86 			if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS)
87 				saw_ns = 1;
88 		}
89 		return saw_ns?VAL_CLASS_REFERRAL:VAL_CLASS_NODATA;
90 	}
91 	/* root referral where NS set is in the answer section */
92 	if(!(query_flags&BIT_RD) && rep->ns_numrrsets == 0 &&
93 		rep->an_numrrsets == 1 && rcode == LDNS_RCODE_NOERROR &&
94 		ntohs(rep->rrsets[0]->rk.type) == LDNS_RR_TYPE_NS &&
95 		query_dname_compare(rep->rrsets[0]->rk.dname,
96 			origqinf->qname) != 0)
97 		return VAL_CLASS_REFERRAL;
98 
99 	/* dump bad messages */
100 	if(rcode != LDNS_RCODE_NOERROR && rcode != LDNS_RCODE_NXDOMAIN)
101 		return VAL_CLASS_UNKNOWN;
102 	/* next check if the skip into the answer section shows no answer */
103 	if(skip>0 && rep->an_numrrsets <= skip)
104 		return VAL_CLASS_CNAMENOANSWER;
105 
106 	/* Next is NODATA */
107 	if(rcode == LDNS_RCODE_NOERROR && rep->an_numrrsets == 0)
108 		return VAL_CLASS_NODATA;
109 
110 	/* We distinguish between CNAME response and other positive/negative
111 	 * responses because CNAME answers require extra processing. */
112 
113 	/* We distinguish between ANY and CNAME or POSITIVE because
114 	 * ANY responses are validated differently. */
115 	if(rcode == LDNS_RCODE_NOERROR && qinf->qtype == LDNS_RR_TYPE_ANY)
116 		return VAL_CLASS_ANY;
117 
118 	/* Note that DNAMEs will be ignored here, unless qtype=DNAME. Unless
119 	 * qtype=CNAME, this will yield a CNAME response. */
120 	for(i=skip; i<rep->an_numrrsets; i++) {
121 		if(rcode == LDNS_RCODE_NOERROR &&
122 			ntohs(rep->rrsets[i]->rk.type) == qinf->qtype)
123 			return VAL_CLASS_POSITIVE;
124 		if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_CNAME)
125 			return VAL_CLASS_CNAME;
126 	}
127 	log_dns_msg("validator: error. failed to classify response message: ",
128 		qinf, rep);
129 	return VAL_CLASS_UNKNOWN;
130 }
131 
132 /** Get signer name from RRSIG */
133 static void
134 rrsig_get_signer(uint8_t* data, size_t len, uint8_t** sname, size_t* slen)
135 {
136 	/* RRSIG rdata is not allowed to be compressed, it is stored
137 	 * uncompressed in memory as well, so return a ptr to the name */
138 	if(len < 21) {
139 		/* too short RRSig:
140 		 * short, byte, byte, long, long, long, short, "." is
141 		 * 2	1	1	4	4  4	2	1 = 19
142 		 * 			and a skip of 18 bytes to the name.
143 		 * +2 for the rdatalen is 21 bytes len for root label */
144 		*sname = NULL;
145 		*slen = 0;
146 		return;
147 	}
148 	data += 20; /* skip the fixed size bits */
149 	len -= 20;
150 	*slen = dname_valid(data, len);
151 	if(!*slen) {
152 		/* bad dname in this rrsig. */
153 		*sname = NULL;
154 		return;
155 	}
156 	*sname = data;
157 }
158 
159 void
160 val_find_rrset_signer(struct ub_packed_rrset_key* rrset, uint8_t** sname,
161 	size_t* slen)
162 {
163 	struct packed_rrset_data* d = (struct packed_rrset_data*)
164 		rrset->entry.data;
165 	/* return signer for first signature, or NULL */
166 	if(d->rrsig_count == 0) {
167 		*sname = NULL;
168 		*slen = 0;
169 		return;
170 	}
171 	/* get rrsig signer name out of the signature */
172 	rrsig_get_signer(d->rr_data[d->count], d->rr_len[d->count],
173 		sname, slen);
174 }
175 
176 /**
177  * Find best signer name in this set of rrsigs.
178  * @param rrset: which rrsigs to look through.
179  * @param qinf: the query name that needs validation.
180  * @param signer_name: the best signer_name. Updated if a better one is found.
181  * @param signer_len: length of signer name.
182  * @param matchcount: count of current best name (starts at 0 for no match).
183  * 	Updated if match is improved.
184  */
185 static void
186 val_find_best_signer(struct ub_packed_rrset_key* rrset,
187 	struct query_info* qinf, uint8_t** signer_name, size_t* signer_len,
188 	int* matchcount)
189 {
190 	struct packed_rrset_data* d = (struct packed_rrset_data*)
191 		rrset->entry.data;
192 	uint8_t* sign;
193 	size_t i;
194 	int m;
195 	for(i=d->count; i<d->count+d->rrsig_count; i++) {
196 		sign = d->rr_data[i]+2+18;
197 		/* look at signatures that are valid (long enough),
198 		 * and have a signer name that is a superdomain of qname,
199 		 * and then check the number of labels in the shared topdomain
200 		 * improve the match if possible */
201 		if(d->rr_len[i] > 2+19 && /* rdata, sig + root label*/
202 			dname_subdomain_c(qinf->qname, sign)) {
203 			(void)dname_lab_cmp(qinf->qname,
204 				dname_count_labels(qinf->qname),
205 				sign, dname_count_labels(sign), &m);
206 			if(m > *matchcount) {
207 				*matchcount = m;
208 				*signer_name = sign;
209 				(void)dname_count_size_labels(*signer_name,
210 					signer_len);
211 			}
212 		}
213 	}
214 }
215 
216 void
217 val_find_signer(enum val_classification subtype, struct query_info* qinf,
218 	struct reply_info* rep, size_t skip, uint8_t** signer_name,
219 	size_t* signer_len)
220 {
221 	size_t i;
222 
223 	if(subtype == VAL_CLASS_POSITIVE) {
224 		/* check for the answer rrset */
225 		for(i=skip; i<rep->an_numrrsets; i++) {
226 			if(query_dname_compare(qinf->qname,
227 				rep->rrsets[i]->rk.dname) == 0) {
228 				val_find_rrset_signer(rep->rrsets[i],
229 					signer_name, signer_len);
230 				return;
231 			}
232 		}
233 		*signer_name = NULL;
234 		*signer_len = 0;
235 	} else if(subtype == VAL_CLASS_CNAME) {
236 		/* check for the first signed cname/dname rrset */
237 		for(i=skip; i<rep->an_numrrsets; i++) {
238 			val_find_rrset_signer(rep->rrsets[i],
239 				signer_name, signer_len);
240 			if(*signer_name)
241 				return;
242 			if(ntohs(rep->rrsets[i]->rk.type) != LDNS_RR_TYPE_DNAME)
243 				break; /* only check CNAME after a DNAME */
244 		}
245 		*signer_name = NULL;
246 		*signer_len = 0;
247 	} else if(subtype == VAL_CLASS_NAMEERROR
248 		|| subtype == VAL_CLASS_NODATA) {
249 		/*Check to see if the AUTH section NSEC record(s) have rrsigs*/
250 		for(i=rep->an_numrrsets; i<
251 			rep->an_numrrsets+rep->ns_numrrsets; i++) {
252 			if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NSEC
253 				|| ntohs(rep->rrsets[i]->rk.type) ==
254 				LDNS_RR_TYPE_NSEC3) {
255 				val_find_rrset_signer(rep->rrsets[i],
256 					signer_name, signer_len);
257 				return;
258 			}
259 		}
260 	} else if(subtype == VAL_CLASS_CNAMENOANSWER) {
261 		/* find closest superdomain signer name in authority section
262 		 * NSEC and NSEC3s */
263 		int matchcount = 0;
264 		*signer_name = NULL;
265 		*signer_len = 0;
266 		for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->
267 			ns_numrrsets; i++) {
268 			if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NSEC
269 				|| ntohs(rep->rrsets[i]->rk.type) ==
270 				LDNS_RR_TYPE_NSEC3) {
271 				val_find_best_signer(rep->rrsets[i], qinf,
272 					signer_name, signer_len, &matchcount);
273 			}
274 		}
275 	} else if(subtype == VAL_CLASS_ANY) {
276 		/* check for one of the answer rrset that has signatures,
277 		 * or potentially a DNAME is in use with a different qname */
278 		for(i=skip; i<rep->an_numrrsets; i++) {
279 			if(query_dname_compare(qinf->qname,
280 				rep->rrsets[i]->rk.dname) == 0) {
281 				val_find_rrset_signer(rep->rrsets[i],
282 					signer_name, signer_len);
283 				if(*signer_name)
284 					return;
285 			}
286 		}
287 		/* no answer RRSIGs with qname, try a DNAME */
288 		if(skip < rep->an_numrrsets &&
289 			ntohs(rep->rrsets[skip]->rk.type) ==
290 			LDNS_RR_TYPE_DNAME) {
291 			val_find_rrset_signer(rep->rrsets[skip],
292 				signer_name, signer_len);
293 			if(*signer_name)
294 				return;
295 		}
296 		*signer_name = NULL;
297 		*signer_len = 0;
298 	} else if(subtype == VAL_CLASS_REFERRAL) {
299 		/* find keys for the item at skip */
300 		if(skip < rep->rrset_count) {
301 			val_find_rrset_signer(rep->rrsets[skip],
302 				signer_name, signer_len);
303 			return;
304 		}
305 		*signer_name = NULL;
306 		*signer_len = 0;
307 	} else {
308 		verbose(VERB_QUERY, "find_signer: could not find signer name"
309 			" for unknown type response");
310 		*signer_name = NULL;
311 		*signer_len = 0;
312 	}
313 }
314 
315 /** return number of rrs in an rrset */
316 static size_t
317 rrset_get_count(struct ub_packed_rrset_key* rrset)
318 {
319 	struct packed_rrset_data* d = (struct packed_rrset_data*)
320 		rrset->entry.data;
321 	if(!d) return 0;
322 	return d->count;
323 }
324 
325 /** return TTL of rrset */
326 static uint32_t
327 rrset_get_ttl(struct ub_packed_rrset_key* rrset)
328 {
329 	struct packed_rrset_data* d = (struct packed_rrset_data*)
330 		rrset->entry.data;
331 	if(!d) return 0;
332 	return d->ttl;
333 }
334 
335 enum sec_status
336 val_verify_rrset(struct module_env* env, struct val_env* ve,
337         struct ub_packed_rrset_key* rrset, struct ub_packed_rrset_key* keys,
338 	uint8_t* sigalg, char** reason, sldns_pkt_section section,
339 	struct module_qstate* qstate)
340 {
341 	enum sec_status sec;
342 	struct packed_rrset_data* d = (struct packed_rrset_data*)rrset->
343 		entry.data;
344 	if(d->security == sec_status_secure) {
345 		/* re-verify all other statuses, because keyset may change*/
346 		log_nametypeclass(VERB_ALGO, "verify rrset cached",
347 			rrset->rk.dname, ntohs(rrset->rk.type),
348 			ntohs(rrset->rk.rrset_class));
349 		return d->security;
350 	}
351 	/* check in the cache if verification has already been done */
352 	rrset_check_sec_status(env->rrset_cache, rrset, *env->now);
353 	if(d->security == sec_status_secure) {
354 		log_nametypeclass(VERB_ALGO, "verify rrset from cache",
355 			rrset->rk.dname, ntohs(rrset->rk.type),
356 			ntohs(rrset->rk.rrset_class));
357 		return d->security;
358 	}
359 	log_nametypeclass(VERB_ALGO, "verify rrset", rrset->rk.dname,
360 		ntohs(rrset->rk.type), ntohs(rrset->rk.rrset_class));
361 	sec = dnskeyset_verify_rrset(env, ve, rrset, keys, sigalg, reason,
362 		section, qstate);
363 	verbose(VERB_ALGO, "verify result: %s", sec_status_to_string(sec));
364 	regional_free_all(env->scratch);
365 
366 	/* update rrset security status
367 	 * only improves security status
368 	 * and bogus is set only once, even if we rechecked the status */
369 	if(sec > d->security) {
370 		d->security = sec;
371 		if(sec == sec_status_secure)
372 			d->trust = rrset_trust_validated;
373 		else if(sec == sec_status_bogus) {
374 			size_t i;
375 			/* update ttl for rrset to fixed value. */
376 			d->ttl = ve->bogus_ttl;
377 			for(i=0; i<d->count+d->rrsig_count; i++)
378 				d->rr_ttl[i] = ve->bogus_ttl;
379 			/* leave RR specific TTL: not used for determine
380 			 * if RRset timed out and clients see proper value. */
381 			lock_basic_lock(&ve->bogus_lock);
382 			ve->num_rrset_bogus++;
383 			lock_basic_unlock(&ve->bogus_lock);
384 		}
385 		/* if status updated - store in cache for reuse */
386 		rrset_update_sec_status(env->rrset_cache, rrset, *env->now);
387 	}
388 
389 	return sec;
390 }
391 
392 enum sec_status
393 val_verify_rrset_entry(struct module_env* env, struct val_env* ve,
394         struct ub_packed_rrset_key* rrset, struct key_entry_key* kkey,
395 	char** reason, sldns_pkt_section section, struct module_qstate* qstate)
396 {
397 	/* temporary dnskey rrset-key */
398 	struct ub_packed_rrset_key dnskey;
399 	struct key_entry_data* kd = (struct key_entry_data*)kkey->entry.data;
400 	enum sec_status sec;
401 	dnskey.rk.type = htons(kd->rrset_type);
402 	dnskey.rk.rrset_class = htons(kkey->key_class);
403 	dnskey.rk.flags = 0;
404 	dnskey.rk.dname = kkey->name;
405 	dnskey.rk.dname_len = kkey->namelen;
406 	dnskey.entry.key = &dnskey;
407 	dnskey.entry.data = kd->rrset_data;
408 	sec = val_verify_rrset(env, ve, rrset, &dnskey, kd->algo, reason,
409 		section, qstate);
410 	return sec;
411 }
412 
413 /** verify that a DS RR hashes to a key and that key signs the set */
414 static enum sec_status
415 verify_dnskeys_with_ds_rr(struct module_env* env, struct val_env* ve,
416 	struct ub_packed_rrset_key* dnskey_rrset,
417         struct ub_packed_rrset_key* ds_rrset, size_t ds_idx, char** reason,
418 	struct module_qstate* qstate)
419 {
420 	enum sec_status sec = sec_status_bogus;
421 	size_t i, num, numchecked = 0, numhashok = 0, numsizesupp = 0;
422 	num = rrset_get_count(dnskey_rrset);
423 	for(i=0; i<num; i++) {
424 		/* Skip DNSKEYs that don't match the basic criteria. */
425 		if(ds_get_key_algo(ds_rrset, ds_idx)
426 		   != dnskey_get_algo(dnskey_rrset, i)
427 		   || dnskey_calc_keytag(dnskey_rrset, i)
428 		   != ds_get_keytag(ds_rrset, ds_idx)) {
429 			continue;
430 		}
431 		numchecked++;
432 		verbose(VERB_ALGO, "attempt DS match algo %d keytag %d",
433 			ds_get_key_algo(ds_rrset, ds_idx),
434 			ds_get_keytag(ds_rrset, ds_idx));
435 
436 		/* Convert the candidate DNSKEY into a hash using the
437 		 * same DS hash algorithm. */
438 		if(!ds_digest_match_dnskey(env, dnskey_rrset, i, ds_rrset,
439 			ds_idx)) {
440 			verbose(VERB_ALGO, "DS match attempt failed");
441 			continue;
442 		}
443 		numhashok++;
444 		if(!dnskey_size_is_supported(dnskey_rrset, i)) {
445 			verbose(VERB_ALGO, "DS okay but that DNSKEY size is not supported");
446 			numsizesupp++;
447 			continue;
448 		}
449 		verbose(VERB_ALGO, "DS match digest ok, trying signature");
450 
451 		/* Otherwise, we have a match! Make sure that the DNSKEY
452 		 * verifies *with this key*  */
453 		sec = dnskey_verify_rrset(env, ve, dnskey_rrset,
454 			dnskey_rrset, i, reason, LDNS_SECTION_ANSWER, qstate);
455 		if(sec == sec_status_secure) {
456 			return sec;
457 		}
458 		/* If it didn't validate with the DNSKEY, try the next one! */
459 	}
460 	if(numsizesupp != 0) {
461 		/* there is a working DS, but that DNSKEY is not supported */
462 		return sec_status_insecure;
463 	}
464 	if(numchecked == 0)
465 		algo_needs_reason(env, ds_get_key_algo(ds_rrset, ds_idx),
466 			reason, "no keys have a DS");
467 	else if(numhashok == 0)
468 		*reason = "DS hash mismatches key";
469 	else if(!*reason)
470 		*reason = "keyset not secured by DNSKEY that matches DS";
471 	return sec_status_bogus;
472 }
473 
474 int val_favorite_ds_algo(struct ub_packed_rrset_key* ds_rrset)
475 {
476 	size_t i, num = rrset_get_count(ds_rrset);
477 	int d, digest_algo = 0; /* DS digest algo 0 is not used. */
478 	/* find favorite algo, for now, highest number supported */
479 	for(i=0; i<num; i++) {
480 		if(!ds_digest_algo_is_supported(ds_rrset, i) ||
481 			!ds_key_algo_is_supported(ds_rrset, i)) {
482 			continue;
483 		}
484 		d = ds_get_digest_algo(ds_rrset, i);
485 		if(d > digest_algo)
486 			digest_algo = d;
487 	}
488 	return digest_algo;
489 }
490 
491 enum sec_status
492 val_verify_DNSKEY_with_DS(struct module_env* env, struct val_env* ve,
493 	struct ub_packed_rrset_key* dnskey_rrset,
494 	struct ub_packed_rrset_key* ds_rrset, uint8_t* sigalg, char** reason,
495 	struct module_qstate* qstate)
496 {
497 	/* as long as this is false, we can consider this DS rrset to be
498 	 * equivalent to no DS rrset. */
499 	int has_useful_ds = 0, digest_algo, alg;
500 	struct algo_needs needs;
501 	size_t i, num;
502 	enum sec_status sec;
503 
504 	if(dnskey_rrset->rk.dname_len != ds_rrset->rk.dname_len ||
505 		query_dname_compare(dnskey_rrset->rk.dname, ds_rrset->rk.dname)
506 		!= 0) {
507 		verbose(VERB_QUERY, "DNSKEY RRset did not match DS RRset "
508 			"by name");
509 		*reason = "DNSKEY RRset did not match DS RRset by name";
510 		return sec_status_bogus;
511 	}
512 
513 	if(sigalg) {
514 		/* harden against algo downgrade is enabled */
515 		digest_algo = val_favorite_ds_algo(ds_rrset);
516 		algo_needs_init_ds(&needs, ds_rrset, digest_algo, sigalg);
517 	} else {
518 		/* accept any key algo, any digest algo */
519 		digest_algo = -1;
520 	}
521 	num = rrset_get_count(ds_rrset);
522 	for(i=0; i<num; i++) {
523 		/* Check to see if we can understand this DS.
524 		 * And check it is the strongest digest */
525 		if(!ds_digest_algo_is_supported(ds_rrset, i) ||
526 			!ds_key_algo_is_supported(ds_rrset, i) ||
527 			(sigalg && (ds_get_digest_algo(ds_rrset, i) != digest_algo))) {
528 			continue;
529 		}
530 
531 		sec = verify_dnskeys_with_ds_rr(env, ve, dnskey_rrset,
532 			ds_rrset, i, reason, qstate);
533 		if(sec == sec_status_insecure)
534 			continue;
535 
536 		/* Once we see a single DS with a known digestID and
537 		 * algorithm, we cannot return INSECURE (with a
538 		 * "null" KeyEntry). */
539 		has_useful_ds = 1;
540 
541 		if(sec == sec_status_secure) {
542 			if(!sigalg || algo_needs_set_secure(&needs,
543 				(uint8_t)ds_get_key_algo(ds_rrset, i))) {
544 				verbose(VERB_ALGO, "DS matched DNSKEY.");
545 				if(!dnskeyset_size_is_supported(dnskey_rrset)) {
546 					verbose(VERB_ALGO, "DS works, but dnskeyset contain keys that are unsupported, treat as insecure");
547 					return sec_status_insecure;
548 				}
549 				return sec_status_secure;
550 			}
551 		} else if(sigalg && sec == sec_status_bogus) {
552 			algo_needs_set_bogus(&needs,
553 				(uint8_t)ds_get_key_algo(ds_rrset, i));
554 		}
555 	}
556 
557 	/* None of the DS's worked out. */
558 
559 	/* If no DSs were understandable, then this is OK. */
560 	if(!has_useful_ds) {
561 		verbose(VERB_ALGO, "No usable DS records were found -- "
562 			"treating as insecure.");
563 		return sec_status_insecure;
564 	}
565 	/* If any were understandable, then it is bad. */
566 	verbose(VERB_QUERY, "Failed to match any usable DS to a DNSKEY.");
567 	if(sigalg && (alg=algo_needs_missing(&needs)) != 0) {
568 		algo_needs_reason(env, alg, reason, "missing verification of "
569 			"DNSKEY signature");
570 	}
571 	return sec_status_bogus;
572 }
573 
574 struct key_entry_key*
575 val_verify_new_DNSKEYs(struct regional* region, struct module_env* env,
576 	struct val_env* ve, struct ub_packed_rrset_key* dnskey_rrset,
577 	struct ub_packed_rrset_key* ds_rrset, int downprot, char** reason,
578 	struct module_qstate* qstate)
579 {
580 	uint8_t sigalg[ALGO_NEEDS_MAX+1];
581 	enum sec_status sec = val_verify_DNSKEY_with_DS(env, ve,
582 		dnskey_rrset, ds_rrset, downprot?sigalg:NULL, reason, qstate);
583 
584 	if(sec == sec_status_secure) {
585 		return key_entry_create_rrset(region,
586 			ds_rrset->rk.dname, ds_rrset->rk.dname_len,
587 			ntohs(ds_rrset->rk.rrset_class), dnskey_rrset,
588 			downprot?sigalg:NULL, *env->now);
589 	} else if(sec == sec_status_insecure) {
590 		return key_entry_create_null(region, ds_rrset->rk.dname,
591 			ds_rrset->rk.dname_len,
592 			ntohs(ds_rrset->rk.rrset_class),
593 			rrset_get_ttl(ds_rrset), *env->now);
594 	}
595 	return key_entry_create_bad(region, ds_rrset->rk.dname,
596 		ds_rrset->rk.dname_len, ntohs(ds_rrset->rk.rrset_class),
597 		BOGUS_KEY_TTL, *env->now);
598 }
599 
600 enum sec_status
601 val_verify_DNSKEY_with_TA(struct module_env* env, struct val_env* ve,
602 	struct ub_packed_rrset_key* dnskey_rrset,
603 	struct ub_packed_rrset_key* ta_ds,
604 	struct ub_packed_rrset_key* ta_dnskey, uint8_t* sigalg, char** reason,
605 	struct module_qstate* qstate)
606 {
607 	/* as long as this is false, we can consider this anchor to be
608 	 * equivalent to no anchor. */
609 	int has_useful_ta = 0, digest_algo = 0, alg;
610 	struct algo_needs needs;
611 	size_t i, num;
612 	enum sec_status sec;
613 
614 	if(ta_ds && (dnskey_rrset->rk.dname_len != ta_ds->rk.dname_len ||
615 		query_dname_compare(dnskey_rrset->rk.dname, ta_ds->rk.dname)
616 		!= 0)) {
617 		verbose(VERB_QUERY, "DNSKEY RRset did not match DS RRset "
618 			"by name");
619 		*reason = "DNSKEY RRset did not match DS RRset by name";
620 		return sec_status_bogus;
621 	}
622 	if(ta_dnskey && (dnskey_rrset->rk.dname_len != ta_dnskey->rk.dname_len
623 	     || query_dname_compare(dnskey_rrset->rk.dname, ta_dnskey->rk.dname)
624 		!= 0)) {
625 		verbose(VERB_QUERY, "DNSKEY RRset did not match anchor RRset "
626 			"by name");
627 		*reason = "DNSKEY RRset did not match anchor RRset by name";
628 		return sec_status_bogus;
629 	}
630 
631 	if(ta_ds)
632 		digest_algo = val_favorite_ds_algo(ta_ds);
633 	if(sigalg) {
634 		if(ta_ds)
635 			algo_needs_init_ds(&needs, ta_ds, digest_algo, sigalg);
636 		else	memset(&needs, 0, sizeof(needs));
637 		if(ta_dnskey)
638 			algo_needs_init_dnskey_add(&needs, ta_dnskey, sigalg);
639 	}
640 	if(ta_ds) {
641 	    num = rrset_get_count(ta_ds);
642 	    for(i=0; i<num; i++) {
643 		/* Check to see if we can understand this DS.
644 		 * And check it is the strongest digest */
645 		if(!ds_digest_algo_is_supported(ta_ds, i) ||
646 			!ds_key_algo_is_supported(ta_ds, i) ||
647 			ds_get_digest_algo(ta_ds, i) != digest_algo)
648 			continue;
649 
650 		sec = verify_dnskeys_with_ds_rr(env, ve, dnskey_rrset,
651 			ta_ds, i, reason, qstate);
652 		if(sec == sec_status_insecure)
653 			continue;
654 
655 		/* Once we see a single DS with a known digestID and
656 		 * algorithm, we cannot return INSECURE (with a
657 		 * "null" KeyEntry). */
658 		has_useful_ta = 1;
659 
660 		if(sec == sec_status_secure) {
661 			if(!sigalg || algo_needs_set_secure(&needs,
662 				(uint8_t)ds_get_key_algo(ta_ds, i))) {
663 				verbose(VERB_ALGO, "DS matched DNSKEY.");
664 				if(!dnskeyset_size_is_supported(dnskey_rrset)) {
665 					verbose(VERB_ALGO, "trustanchor works, but dnskeyset contain keys that are unsupported, treat as insecure");
666 					return sec_status_insecure;
667 				}
668 				return sec_status_secure;
669 			}
670 		} else if(sigalg && sec == sec_status_bogus) {
671 			algo_needs_set_bogus(&needs,
672 				(uint8_t)ds_get_key_algo(ta_ds, i));
673 		}
674 	    }
675 	}
676 
677 	/* None of the DS's worked out: check the DNSKEYs. */
678 	if(ta_dnskey) {
679 	    num = rrset_get_count(ta_dnskey);
680 	    for(i=0; i<num; i++) {
681 		/* Check to see if we can understand this DNSKEY */
682 		if(!dnskey_algo_is_supported(ta_dnskey, i))
683 			continue;
684 		if(!dnskey_size_is_supported(ta_dnskey, i))
685 			continue;
686 
687 		/* we saw a useful TA */
688 		has_useful_ta = 1;
689 
690 		sec = dnskey_verify_rrset(env, ve, dnskey_rrset,
691 			ta_dnskey, i, reason, LDNS_SECTION_ANSWER, qstate);
692 		if(sec == sec_status_secure) {
693 			if(!sigalg || algo_needs_set_secure(&needs,
694 				(uint8_t)dnskey_get_algo(ta_dnskey, i))) {
695 				verbose(VERB_ALGO, "anchor matched DNSKEY.");
696 				if(!dnskeyset_size_is_supported(dnskey_rrset)) {
697 					verbose(VERB_ALGO, "trustanchor works, but dnskeyset contain keys that are unsupported, treat as insecure");
698 					return sec_status_insecure;
699 				}
700 				return sec_status_secure;
701 			}
702 		} else if(sigalg && sec == sec_status_bogus) {
703 			algo_needs_set_bogus(&needs,
704 				(uint8_t)dnskey_get_algo(ta_dnskey, i));
705 		}
706 	    }
707 	}
708 
709 	/* If no DSs were understandable, then this is OK. */
710 	if(!has_useful_ta) {
711 		verbose(VERB_ALGO, "No usable trust anchors were found -- "
712 			"treating as insecure.");
713 		return sec_status_insecure;
714 	}
715 	/* If any were understandable, then it is bad. */
716 	verbose(VERB_QUERY, "Failed to match any usable anchor to a DNSKEY.");
717 	if(sigalg && (alg=algo_needs_missing(&needs)) != 0) {
718 		algo_needs_reason(env, alg, reason, "missing verification of "
719 			"DNSKEY signature");
720 	}
721 	return sec_status_bogus;
722 }
723 
724 struct key_entry_key*
725 val_verify_new_DNSKEYs_with_ta(struct regional* region, struct module_env* env,
726 	struct val_env* ve, struct ub_packed_rrset_key* dnskey_rrset,
727 	struct ub_packed_rrset_key* ta_ds_rrset,
728 	struct ub_packed_rrset_key* ta_dnskey_rrset, int downprot,
729 	char** reason, struct module_qstate* qstate)
730 {
731 	uint8_t sigalg[ALGO_NEEDS_MAX+1];
732 	enum sec_status sec = val_verify_DNSKEY_with_TA(env, ve,
733 		dnskey_rrset, ta_ds_rrset, ta_dnskey_rrset,
734 		downprot?sigalg:NULL, reason, qstate);
735 
736 	if(sec == sec_status_secure) {
737 		return key_entry_create_rrset(region,
738 			dnskey_rrset->rk.dname, dnskey_rrset->rk.dname_len,
739 			ntohs(dnskey_rrset->rk.rrset_class), dnskey_rrset,
740 			downprot?sigalg:NULL, *env->now);
741 	} else if(sec == sec_status_insecure) {
742 		return key_entry_create_null(region, dnskey_rrset->rk.dname,
743 			dnskey_rrset->rk.dname_len,
744 			ntohs(dnskey_rrset->rk.rrset_class),
745 			rrset_get_ttl(dnskey_rrset), *env->now);
746 	}
747 	return key_entry_create_bad(region, dnskey_rrset->rk.dname,
748 		dnskey_rrset->rk.dname_len, ntohs(dnskey_rrset->rk.rrset_class),
749 		BOGUS_KEY_TTL, *env->now);
750 }
751 
752 int
753 val_dsset_isusable(struct ub_packed_rrset_key* ds_rrset)
754 {
755 	size_t i;
756 	for(i=0; i<rrset_get_count(ds_rrset); i++) {
757 		if(ds_digest_algo_is_supported(ds_rrset, i) &&
758 			ds_key_algo_is_supported(ds_rrset, i))
759 			return 1;
760 	}
761 	if(verbosity < VERB_ALGO)
762 		return 0;
763 	if(rrset_get_count(ds_rrset) == 0)
764 		verbose(VERB_ALGO, "DS is not usable");
765 	else {
766 		/* report usability for the first DS RR */
767 		sldns_lookup_table *lt;
768 		char herr[64], aerr[64];
769 		lt = sldns_lookup_by_id(sldns_hashes,
770 			(int)ds_get_digest_algo(ds_rrset, i));
771 		if(lt) snprintf(herr, sizeof(herr), "%s", lt->name);
772 		else snprintf(herr, sizeof(herr), "%d",
773 			(int)ds_get_digest_algo(ds_rrset, i));
774 		lt = sldns_lookup_by_id(sldns_algorithms,
775 			(int)ds_get_key_algo(ds_rrset, i));
776 		if(lt) snprintf(aerr, sizeof(aerr), "%s", lt->name);
777 		else snprintf(aerr, sizeof(aerr), "%d",
778 			(int)ds_get_key_algo(ds_rrset, i));
779 		verbose(VERB_ALGO, "DS unsupported, hash %s %s, "
780 			"key algorithm %s %s", herr,
781 			(ds_digest_algo_is_supported(ds_rrset, 0)?
782 			"(supported)":"(unsupported)"), aerr,
783 			(ds_key_algo_is_supported(ds_rrset, 0)?
784 			"(supported)":"(unsupported)"));
785 	}
786 	return 0;
787 }
788 
789 /** get label count for a signature */
790 static uint8_t
791 rrsig_get_labcount(struct packed_rrset_data* d, size_t sig)
792 {
793 	if(d->rr_len[sig] < 2+4)
794 		return 0; /* bad sig length */
795 	return d->rr_data[sig][2+3];
796 }
797 
798 int
799 val_rrset_wildcard(struct ub_packed_rrset_key* rrset, uint8_t** wc,
800 	size_t* wc_len)
801 {
802 	struct packed_rrset_data* d = (struct packed_rrset_data*)rrset->
803 		entry.data;
804 	uint8_t labcount;
805 	int labdiff;
806 	uint8_t* wn;
807 	size_t i, wl;
808 	if(d->rrsig_count == 0) {
809 		return 1;
810 	}
811 	labcount = rrsig_get_labcount(d, d->count + 0);
812 	/* check rest of signatures identical */
813 	for(i=1; i<d->rrsig_count; i++) {
814 		if(labcount != rrsig_get_labcount(d, d->count + i)) {
815 			return 0;
816 		}
817 	}
818 	/* OK the rrsigs check out */
819 	/* if the RRSIG label count is shorter than the number of actual
820 	 * labels, then this rrset was synthesized from a wildcard.
821 	 * Note that the RRSIG label count doesn't count the root label. */
822 	wn = rrset->rk.dname;
823 	wl = rrset->rk.dname_len;
824 	/* skip a leading wildcard label in the dname (RFC4035 2.2) */
825 	if(dname_is_wild(wn)) {
826 		wn += 2;
827 		wl -= 2;
828 	}
829 	labdiff = (dname_count_labels(wn) - 1) - (int)labcount;
830 	if(labdiff > 0) {
831 		*wc = wn;
832 		dname_remove_labels(wc, &wl, labdiff);
833 		*wc_len = wl;
834 		return 1;
835 	}
836 	return 1;
837 }
838 
839 int
840 val_chase_cname(struct query_info* qchase, struct reply_info* rep,
841 	size_t* cname_skip) {
842 	size_t i;
843 	/* skip any DNAMEs, go to the CNAME for next part */
844 	for(i = *cname_skip; i < rep->an_numrrsets; i++) {
845 		if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_CNAME &&
846 			query_dname_compare(qchase->qname, rep->rrsets[i]->
847 				rk.dname) == 0) {
848 			qchase->qname = NULL;
849 			get_cname_target(rep->rrsets[i], &qchase->qname,
850 				&qchase->qname_len);
851 			if(!qchase->qname)
852 				return 0; /* bad CNAME rdata */
853 			(*cname_skip) = i+1;
854 			return 1;
855 		}
856 	}
857 	return 0; /* CNAME classified but no matching CNAME ?! */
858 }
859 
860 /** see if rrset has signer name as one of the rrsig signers */
861 static int
862 rrset_has_signer(struct ub_packed_rrset_key* rrset, uint8_t* name, size_t len)
863 {
864 	struct packed_rrset_data* d = (struct packed_rrset_data*)rrset->
865 		entry.data;
866 	size_t i;
867 	for(i = d->count; i< d->count+d->rrsig_count; i++) {
868 		if(d->rr_len[i] > 2+18+len) {
869 			/* at least rdatalen + signature + signame (+1 sig)*/
870 			if(!dname_valid(d->rr_data[i]+2+18, d->rr_len[i]-2-18))
871 				continue;
872 			if(query_dname_compare(name, d->rr_data[i]+2+18) == 0)
873 			{
874 				return 1;
875 			}
876 		}
877 	}
878 	return 0;
879 }
880 
881 void
882 val_fill_reply(struct reply_info* chase, struct reply_info* orig,
883 	size_t skip, uint8_t* name, size_t len, uint8_t* signer)
884 {
885 	size_t i;
886 	int seen_dname = 0;
887 	chase->rrset_count = 0;
888 	chase->an_numrrsets = 0;
889 	chase->ns_numrrsets = 0;
890 	chase->ar_numrrsets = 0;
891 	/* ANSWER section */
892 	for(i=skip; i<orig->an_numrrsets; i++) {
893 		if(!signer) {
894 			if(query_dname_compare(name,
895 				orig->rrsets[i]->rk.dname) == 0)
896 				chase->rrsets[chase->an_numrrsets++] =
897 					orig->rrsets[i];
898 		} else if(seen_dname && ntohs(orig->rrsets[i]->rk.type) ==
899 			LDNS_RR_TYPE_CNAME) {
900 			chase->rrsets[chase->an_numrrsets++] = orig->rrsets[i];
901 			seen_dname = 0;
902 		} else if(rrset_has_signer(orig->rrsets[i], name, len)) {
903 			chase->rrsets[chase->an_numrrsets++] = orig->rrsets[i];
904 			if(ntohs(orig->rrsets[i]->rk.type) ==
905 				LDNS_RR_TYPE_DNAME) {
906 					seen_dname = 1;
907 			}
908 		}
909 	}
910 	/* AUTHORITY section */
911 	for(i = (skip > orig->an_numrrsets)?skip:orig->an_numrrsets;
912 		i<orig->an_numrrsets+orig->ns_numrrsets;
913 		i++) {
914 		if(!signer) {
915 			if(query_dname_compare(name,
916 				orig->rrsets[i]->rk.dname) == 0)
917 				chase->rrsets[chase->an_numrrsets+
918 				    chase->ns_numrrsets++] = orig->rrsets[i];
919 		} else if(rrset_has_signer(orig->rrsets[i], name, len)) {
920 			chase->rrsets[chase->an_numrrsets+
921 				chase->ns_numrrsets++] = orig->rrsets[i];
922 		}
923 	}
924 	/* ADDITIONAL section */
925 	for(i= (skip>orig->an_numrrsets+orig->ns_numrrsets)?
926 		skip:orig->an_numrrsets+orig->ns_numrrsets;
927 		i<orig->rrset_count; i++) {
928 		if(!signer) {
929 			if(query_dname_compare(name,
930 				orig->rrsets[i]->rk.dname) == 0)
931 			    chase->rrsets[chase->an_numrrsets
932 				+orig->ns_numrrsets+chase->ar_numrrsets++]
933 				= orig->rrsets[i];
934 		} else if(rrset_has_signer(orig->rrsets[i], name, len)) {
935 			chase->rrsets[chase->an_numrrsets+orig->ns_numrrsets+
936 				chase->ar_numrrsets++] = orig->rrsets[i];
937 		}
938 	}
939 	chase->rrset_count = chase->an_numrrsets + chase->ns_numrrsets +
940 		chase->ar_numrrsets;
941 }
942 
943 void val_reply_remove_auth(struct reply_info* rep, size_t index)
944 {
945 	log_assert(index < rep->rrset_count);
946 	log_assert(index >= rep->an_numrrsets);
947 	log_assert(index < rep->an_numrrsets+rep->ns_numrrsets);
948 	memmove(rep->rrsets+index, rep->rrsets+index+1,
949 		sizeof(struct ub_packed_rrset_key*)*
950 		(rep->rrset_count - index - 1));
951 	rep->ns_numrrsets--;
952 	rep->rrset_count--;
953 }
954 
955 void
956 val_check_nonsecure(struct module_env* env, struct reply_info* rep)
957 {
958 	size_t i;
959 	/* authority */
960 	for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
961 		if(((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
962 			->security != sec_status_secure) {
963 			/* because we want to return the authentic original
964 			 * message when presented with CD-flagged queries,
965 			 * we need to preserve AUTHORITY section data.
966 			 * However, this rrset is not signed or signed
967 			 * with the wrong keys. Validation has tried to
968 			 * verify this rrset with the keysets of import.
969 			 * But this rrset did not verify.
970 			 * Therefore the message is bogus.
971 			 */
972 
973 			/* check if authority has an NS record
974 			 * which is bad, and there is an answer section with
975 			 * data.  In that case, delete NS and additional to
976 			 * be lenient and make a minimal response */
977 			if(rep->an_numrrsets != 0 &&
978 				ntohs(rep->rrsets[i]->rk.type)
979 				== LDNS_RR_TYPE_NS) {
980 				verbose(VERB_ALGO, "truncate to minimal");
981 				rep->ar_numrrsets = 0;
982 				rep->rrset_count = rep->an_numrrsets +
983 					rep->ns_numrrsets;
984 				/* remove this unneeded authority rrset */
985 				memmove(rep->rrsets+i, rep->rrsets+i+1,
986 					sizeof(struct ub_packed_rrset_key*)*
987 					(rep->rrset_count - i - 1));
988 				rep->ns_numrrsets--;
989 				rep->rrset_count--;
990 				i--;
991 				return;
992 			}
993 
994 			log_nametypeclass(VERB_QUERY, "message is bogus, "
995 				"non secure rrset",
996 				rep->rrsets[i]->rk.dname,
997 				ntohs(rep->rrsets[i]->rk.type),
998 				ntohs(rep->rrsets[i]->rk.rrset_class));
999 			rep->security = sec_status_bogus;
1000 			return;
1001 		}
1002 	}
1003 	/* additional */
1004 	if(!env->cfg->val_clean_additional)
1005 		return;
1006 	for(i=rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) {
1007 		if(((struct packed_rrset_data*)rep->rrsets[i]->entry.data)
1008 			->security != sec_status_secure) {
1009 			/* This does not cause message invalidation. It was
1010 			 * simply unsigned data in the additional. The
1011 			 * RRSIG must have been truncated off the message.
1012 			 *
1013 			 * However, we do not want to return possible bogus
1014 			 * data to clients that rely on this service for
1015 			 * their authentication.
1016 			 */
1017 			/* remove this unneeded additional rrset */
1018 			memmove(rep->rrsets+i, rep->rrsets+i+1,
1019 				sizeof(struct ub_packed_rrset_key*)*
1020 				(rep->rrset_count - i - 1));
1021 			rep->ar_numrrsets--;
1022 			rep->rrset_count--;
1023 			i--;
1024 		}
1025 	}
1026 }
1027 
1028 /** check no anchor and unlock */
1029 static int
1030 check_no_anchor(struct val_anchors* anchors, uint8_t* nm, size_t l, uint16_t c)
1031 {
1032 	struct trust_anchor* ta;
1033 	if((ta=anchors_lookup(anchors, nm, l, c))) {
1034 		lock_basic_unlock(&ta->lock);
1035 	}
1036 	return !ta;
1037 }
1038 
1039 void
1040 val_mark_indeterminate(struct reply_info* rep, struct val_anchors* anchors,
1041 	struct rrset_cache* r, struct module_env* env)
1042 {
1043 	size_t i;
1044 	struct packed_rrset_data* d;
1045 	for(i=0; i<rep->rrset_count; i++) {
1046 		d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data;
1047 		if(d->security == sec_status_unchecked &&
1048 		   check_no_anchor(anchors, rep->rrsets[i]->rk.dname,
1049 			rep->rrsets[i]->rk.dname_len,
1050 			ntohs(rep->rrsets[i]->rk.rrset_class)))
1051 		{
1052 			/* mark as indeterminate */
1053 			d->security = sec_status_indeterminate;
1054 			rrset_update_sec_status(r, rep->rrsets[i], *env->now);
1055 		}
1056 	}
1057 }
1058 
1059 void
1060 val_mark_insecure(struct reply_info* rep, uint8_t* kname,
1061 	struct rrset_cache* r, struct module_env* env)
1062 {
1063 	size_t i;
1064 	struct packed_rrset_data* d;
1065 	for(i=0; i<rep->rrset_count; i++) {
1066 		d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data;
1067 		if(d->security == sec_status_unchecked &&
1068 		   dname_subdomain_c(rep->rrsets[i]->rk.dname, kname)) {
1069 			/* mark as insecure */
1070 			d->security = sec_status_insecure;
1071 			rrset_update_sec_status(r, rep->rrsets[i], *env->now);
1072 		}
1073 	}
1074 }
1075 
1076 size_t
1077 val_next_unchecked(struct reply_info* rep, size_t skip)
1078 {
1079 	size_t i;
1080 	struct packed_rrset_data* d;
1081 	for(i=skip+1; i<rep->rrset_count; i++) {
1082 		d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data;
1083 		if(d->security == sec_status_unchecked) {
1084 			return i;
1085 		}
1086 	}
1087 	return rep->rrset_count;
1088 }
1089 
1090 const char*
1091 val_classification_to_string(enum val_classification subtype)
1092 {
1093 	switch(subtype) {
1094 		case VAL_CLASS_UNTYPED: 	return "untyped";
1095 		case VAL_CLASS_UNKNOWN: 	return "unknown";
1096 		case VAL_CLASS_POSITIVE: 	return "positive";
1097 		case VAL_CLASS_CNAME: 		return "cname";
1098 		case VAL_CLASS_NODATA: 		return "nodata";
1099 		case VAL_CLASS_NAMEERROR: 	return "nameerror";
1100 		case VAL_CLASS_CNAMENOANSWER: 	return "cnamenoanswer";
1101 		case VAL_CLASS_REFERRAL: 	return "referral";
1102 		case VAL_CLASS_ANY: 		return "qtype_any";
1103 		default:
1104 			return "bad_val_classification";
1105 	}
1106 }
1107 
1108 /** log a sock_list entry */
1109 static void
1110 sock_list_logentry(enum verbosity_value v, const char* s, struct sock_list* p)
1111 {
1112 	if(p->len)
1113 		log_addr(v, s, &p->addr, p->len);
1114 	else	verbose(v, "%s cache", s);
1115 }
1116 
1117 void val_blacklist(struct sock_list** blacklist, struct regional* region,
1118 	struct sock_list* origin, int cross)
1119 {
1120 	/* debug printout */
1121 	if(verbosity >= VERB_ALGO) {
1122 		struct sock_list* p;
1123 		for(p=*blacklist; p; p=p->next)
1124 			sock_list_logentry(VERB_ALGO, "blacklist", p);
1125 		if(!origin)
1126 			verbose(VERB_ALGO, "blacklist add: cache");
1127 		for(p=origin; p; p=p->next)
1128 			sock_list_logentry(VERB_ALGO, "blacklist add", p);
1129 	}
1130 	/* blacklist the IPs or the cache */
1131 	if(!origin) {
1132 		/* only add if nothing there. anything else also stops cache*/
1133 		if(!*blacklist)
1134 			sock_list_insert(blacklist, NULL, 0, region);
1135 	} else if(!cross)
1136 		sock_list_prepend(blacklist, origin);
1137 	else	sock_list_merge(blacklist, region, origin);
1138 }
1139 
1140 int val_has_signed_nsecs(struct reply_info* rep, char** reason)
1141 {
1142 	size_t i, num_nsec = 0, num_nsec3 = 0;
1143 	struct packed_rrset_data* d;
1144 	for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) {
1145 		if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NSEC))
1146 			num_nsec++;
1147 		else if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NSEC3))
1148 			num_nsec3++;
1149 		else continue;
1150 		d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data;
1151 		if(d && d->rrsig_count != 0) {
1152 			return 1;
1153 		}
1154 	}
1155 	if(num_nsec == 0 && num_nsec3 == 0)
1156 		*reason = "no DNSSEC records";
1157 	else if(num_nsec != 0)
1158 		*reason = "no signatures over NSECs";
1159 	else	*reason = "no signatures over NSEC3s";
1160 	return 0;
1161 }
1162 
1163 struct dns_msg*
1164 val_find_DS(struct module_env* env, uint8_t* nm, size_t nmlen, uint16_t c,
1165 	struct regional* region, uint8_t* topname)
1166 {
1167 	struct dns_msg* msg;
1168 	struct query_info qinfo;
1169 	struct ub_packed_rrset_key *rrset = rrset_cache_lookup(
1170 		env->rrset_cache, nm, nmlen, LDNS_RR_TYPE_DS, c, 0,
1171 		*env->now, 0);
1172 	if(rrset) {
1173 		/* DS rrset exists. Return it to the validator immediately*/
1174 		struct ub_packed_rrset_key* copy = packed_rrset_copy_region(
1175 			rrset, region, *env->now);
1176 		lock_rw_unlock(&rrset->entry.lock);
1177 		if(!copy)
1178 			return NULL;
1179 		msg = dns_msg_create(nm, nmlen, LDNS_RR_TYPE_DS, c, region, 1);
1180 		if(!msg)
1181 			return NULL;
1182 		msg->rep->rrsets[0] = copy;
1183 		msg->rep->rrset_count++;
1184 		msg->rep->an_numrrsets++;
1185 		return msg;
1186 	}
1187 	/* lookup in rrset and negative cache for NSEC/NSEC3 */
1188 	qinfo.qname = nm;
1189 	qinfo.qname_len = nmlen;
1190 	qinfo.qtype = LDNS_RR_TYPE_DS;
1191 	qinfo.qclass = c;
1192 	qinfo.local_alias = NULL;
1193 	/* do not add SOA to reply message, it is going to be used internal */
1194 	msg = val_neg_getmsg(env->neg_cache, &qinfo, region, env->rrset_cache,
1195 		env->scratch_buffer, *env->now, 0, topname, env->cfg);
1196 	return msg;
1197 }
1198