xref: /openbsd-src/sbin/unwind/libunbound/validator/val_nsec3.c (revision 7037e34cdfd270b3989fb1829c7cd3439048bd3a)
1ae8c6e27Sflorian /*
2ae8c6e27Sflorian  * validator/val_nsec3.c - validator NSEC3 denial of existence functions.
3ae8c6e27Sflorian  *
4ae8c6e27Sflorian  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5ae8c6e27Sflorian  *
6ae8c6e27Sflorian  * This software is open source.
7ae8c6e27Sflorian  *
8ae8c6e27Sflorian  * Redistribution and use in source and binary forms, with or without
9ae8c6e27Sflorian  * modification, are permitted provided that the following conditions
10ae8c6e27Sflorian  * are met:
11ae8c6e27Sflorian  *
12ae8c6e27Sflorian  * Redistributions of source code must retain the above copyright notice,
13ae8c6e27Sflorian  * this list of conditions and the following disclaimer.
14ae8c6e27Sflorian  *
15ae8c6e27Sflorian  * Redistributions in binary form must reproduce the above copyright notice,
16ae8c6e27Sflorian  * this list of conditions and the following disclaimer in the documentation
17ae8c6e27Sflorian  * and/or other materials provided with the distribution.
18ae8c6e27Sflorian  *
19ae8c6e27Sflorian  * Neither the name of the NLNET LABS nor the names of its contributors may
20ae8c6e27Sflorian  * be used to endorse or promote products derived from this software without
21ae8c6e27Sflorian  * specific prior written permission.
22ae8c6e27Sflorian  *
23ae8c6e27Sflorian  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24ae8c6e27Sflorian  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25ae8c6e27Sflorian  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26ae8c6e27Sflorian  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27ae8c6e27Sflorian  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28ae8c6e27Sflorian  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29ae8c6e27Sflorian  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30ae8c6e27Sflorian  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31ae8c6e27Sflorian  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32ae8c6e27Sflorian  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33ae8c6e27Sflorian  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34ae8c6e27Sflorian  */
35ae8c6e27Sflorian 
36ae8c6e27Sflorian /**
37ae8c6e27Sflorian  * \file
38ae8c6e27Sflorian  *
39ae8c6e27Sflorian  * This file contains helper functions for the validator module.
40ae8c6e27Sflorian  * The functions help with NSEC3 checking, the different NSEC3 proofs
41ae8c6e27Sflorian  * for denial of existence, and proofs for presence of types.
42ae8c6e27Sflorian  */
43ae8c6e27Sflorian #include "config.h"
44ae8c6e27Sflorian #include <ctype.h>
45ae8c6e27Sflorian #include "validator/val_nsec3.h"
46ae8c6e27Sflorian #include "validator/val_secalgo.h"
47ae8c6e27Sflorian #include "validator/validator.h"
48ae8c6e27Sflorian #include "validator/val_kentry.h"
49ae8c6e27Sflorian #include "services/cache/rrset.h"
50ae8c6e27Sflorian #include "util/regional.h"
51ae8c6e27Sflorian #include "util/rbtree.h"
52ae8c6e27Sflorian #include "util/module.h"
53ae8c6e27Sflorian #include "util/net_help.h"
54ae8c6e27Sflorian #include "util/data/packed_rrset.h"
55ae8c6e27Sflorian #include "util/data/dname.h"
56ae8c6e27Sflorian #include "util/data/msgreply.h"
57ae8c6e27Sflorian /* we include nsec.h for the bitmap_has_type function */
58ae8c6e27Sflorian #include "validator/val_nsec.h"
59ae8c6e27Sflorian #include "sldns/sbuffer.h"
60fed3efa7Sflorian #include "util/config_file.h"
61fed3efa7Sflorian 
62fed3efa7Sflorian /**
63fed3efa7Sflorian  * Max number of NSEC3 calculations at once, suspend query for later.
64fed3efa7Sflorian  * 8 is low enough and allows for cases where multiple proofs are needed.
65fed3efa7Sflorian  */
66fed3efa7Sflorian #define MAX_NSEC3_CALCULATIONS 8
67fed3efa7Sflorian /**
68fed3efa7Sflorian  * When all allowed NSEC3 calculations at once resulted in error treat as
69fed3efa7Sflorian  * bogus. NSEC3 hash errors are not cached and this helps breaks loops with
70fed3efa7Sflorian  * erroneous data.
71fed3efa7Sflorian  */
72fed3efa7Sflorian #define MAX_NSEC3_ERRORS -1
73ae8c6e27Sflorian 
74ae8c6e27Sflorian /**
75ae8c6e27Sflorian  * This function we get from ldns-compat or from base system
76ae8c6e27Sflorian  * it returns the number of data bytes stored at the target, or <0 on error.
77ae8c6e27Sflorian  */
78ae8c6e27Sflorian int sldns_b32_ntop_extended_hex(uint8_t const *src, size_t srclength,
79ae8c6e27Sflorian 	char *target, size_t targsize);
80ae8c6e27Sflorian /**
81ae8c6e27Sflorian  * This function we get from ldns-compat or from base system
82ae8c6e27Sflorian  * it returns the number of data bytes stored at the target, or <0 on error.
83ae8c6e27Sflorian  */
84ae8c6e27Sflorian int sldns_b32_pton_extended_hex(char const *src, size_t hashed_owner_str_len,
85ae8c6e27Sflorian 	uint8_t *target, size_t targsize);
86ae8c6e27Sflorian 
87ae8c6e27Sflorian /**
88ae8c6e27Sflorian  * Closest encloser (ce) proof results
89ae8c6e27Sflorian  * Contains the ce and the next-closer (nc) proof.
90ae8c6e27Sflorian  */
91ae8c6e27Sflorian struct ce_response {
92ae8c6e27Sflorian 	/** the closest encloser name */
93ae8c6e27Sflorian 	uint8_t* ce;
94ae8c6e27Sflorian 	/** length of ce */
95ae8c6e27Sflorian 	size_t ce_len;
96ae8c6e27Sflorian 	/** NSEC3 record that proved ce. rrset */
97ae8c6e27Sflorian 	struct ub_packed_rrset_key* ce_rrset;
98ae8c6e27Sflorian 	/** NSEC3 record that proved ce. rr number */
99ae8c6e27Sflorian 	int ce_rr;
100ae8c6e27Sflorian 	/** NSEC3 record that proved nc. rrset */
101ae8c6e27Sflorian 	struct ub_packed_rrset_key* nc_rrset;
102ae8c6e27Sflorian 	/** NSEC3 record that proved nc. rr*/
103ae8c6e27Sflorian 	int nc_rr;
104ae8c6e27Sflorian };
105ae8c6e27Sflorian 
106ae8c6e27Sflorian /**
107ae8c6e27Sflorian  * Filter conditions for NSEC3 proof
108ae8c6e27Sflorian  * Used to iterate over the applicable NSEC3 RRs.
109ae8c6e27Sflorian  */
110ae8c6e27Sflorian struct nsec3_filter {
111ae8c6e27Sflorian 	/** Zone name, only NSEC3 records for this zone are considered */
112ae8c6e27Sflorian 	uint8_t* zone;
113ae8c6e27Sflorian 	/** length of the zonename */
114ae8c6e27Sflorian 	size_t zone_len;
115ae8c6e27Sflorian 	/** the list of NSEC3s to filter; array */
116ae8c6e27Sflorian 	struct ub_packed_rrset_key** list;
117ae8c6e27Sflorian 	/** number of rrsets in list */
118ae8c6e27Sflorian 	size_t num;
119ae8c6e27Sflorian 	/** class of records for the NSEC3, only this class applies */
120ae8c6e27Sflorian 	uint16_t fclass;
121ae8c6e27Sflorian };
122ae8c6e27Sflorian 
123ae8c6e27Sflorian /** return number of rrs in an rrset */
124ae8c6e27Sflorian static size_t
125ae8c6e27Sflorian rrset_get_count(struct ub_packed_rrset_key* rrset)
126ae8c6e27Sflorian {
127ae8c6e27Sflorian         struct packed_rrset_data* d = (struct packed_rrset_data*)
128ae8c6e27Sflorian 	        rrset->entry.data;
129ae8c6e27Sflorian         if(!d) return 0;
130ae8c6e27Sflorian         return d->count;
131ae8c6e27Sflorian }
132ae8c6e27Sflorian 
133ae8c6e27Sflorian /** return if nsec3 RR has unknown flags */
134ae8c6e27Sflorian static int
135ae8c6e27Sflorian nsec3_unknown_flags(struct ub_packed_rrset_key* rrset, int r)
136ae8c6e27Sflorian {
137ae8c6e27Sflorian         struct packed_rrset_data* d = (struct packed_rrset_data*)
138ae8c6e27Sflorian 	        rrset->entry.data;
139ae8c6e27Sflorian 	log_assert(d && r < (int)d->count);
140ae8c6e27Sflorian 	if(d->rr_len[r] < 2+2)
141ae8c6e27Sflorian 		return 0; /* malformed */
142ae8c6e27Sflorian 	return (int)(d->rr_data[r][2+1] & NSEC3_UNKNOWN_FLAGS);
143ae8c6e27Sflorian }
144ae8c6e27Sflorian 
145ae8c6e27Sflorian int
146ae8c6e27Sflorian nsec3_has_optout(struct ub_packed_rrset_key* rrset, int r)
147ae8c6e27Sflorian {
148ae8c6e27Sflorian         struct packed_rrset_data* d = (struct packed_rrset_data*)
149ae8c6e27Sflorian 	        rrset->entry.data;
150ae8c6e27Sflorian 	log_assert(d && r < (int)d->count);
151ae8c6e27Sflorian 	if(d->rr_len[r] < 2+2)
152ae8c6e27Sflorian 		return 0; /* malformed */
153ae8c6e27Sflorian 	return (int)(d->rr_data[r][2+1] & NSEC3_OPTOUT);
154ae8c6e27Sflorian }
155ae8c6e27Sflorian 
156ae8c6e27Sflorian /** return nsec3 RR algorithm */
157ae8c6e27Sflorian static int
158ae8c6e27Sflorian nsec3_get_algo(struct ub_packed_rrset_key* rrset, int r)
159ae8c6e27Sflorian {
160ae8c6e27Sflorian         struct packed_rrset_data* d = (struct packed_rrset_data*)
161ae8c6e27Sflorian 	        rrset->entry.data;
162ae8c6e27Sflorian 	log_assert(d && r < (int)d->count);
163ae8c6e27Sflorian 	if(d->rr_len[r] < 2+1)
164ae8c6e27Sflorian 		return 0; /* malformed */
165ae8c6e27Sflorian 	return (int)(d->rr_data[r][2+0]);
166ae8c6e27Sflorian }
167ae8c6e27Sflorian 
168ae8c6e27Sflorian /** return if nsec3 RR has known algorithm */
169ae8c6e27Sflorian static int
170ae8c6e27Sflorian nsec3_known_algo(struct ub_packed_rrset_key* rrset, int r)
171ae8c6e27Sflorian {
172ae8c6e27Sflorian         struct packed_rrset_data* d = (struct packed_rrset_data*)
173ae8c6e27Sflorian 	        rrset->entry.data;
174ae8c6e27Sflorian 	log_assert(d && r < (int)d->count);
175ae8c6e27Sflorian 	if(d->rr_len[r] < 2+1)
176ae8c6e27Sflorian 		return 0; /* malformed */
177ae8c6e27Sflorian 	switch(d->rr_data[r][2+0]) {
178ae8c6e27Sflorian 		case NSEC3_HASH_SHA1:
179ae8c6e27Sflorian 			return 1;
180ae8c6e27Sflorian 	}
181ae8c6e27Sflorian 	return 0;
182ae8c6e27Sflorian }
183ae8c6e27Sflorian 
184ae8c6e27Sflorian /** return nsec3 RR iteration count */
185ae8c6e27Sflorian static size_t
186ae8c6e27Sflorian nsec3_get_iter(struct ub_packed_rrset_key* rrset, int r)
187ae8c6e27Sflorian {
188ae8c6e27Sflorian 	uint16_t i;
189ae8c6e27Sflorian         struct packed_rrset_data* d = (struct packed_rrset_data*)
190ae8c6e27Sflorian 	        rrset->entry.data;
191ae8c6e27Sflorian 	log_assert(d && r < (int)d->count);
192ae8c6e27Sflorian 	if(d->rr_len[r] < 2+4)
193ae8c6e27Sflorian 		return 0; /* malformed */
194ae8c6e27Sflorian 	memmove(&i, d->rr_data[r]+2+2, sizeof(i));
195ae8c6e27Sflorian 	i = ntohs(i);
196ae8c6e27Sflorian 	return (size_t)i;
197ae8c6e27Sflorian }
198ae8c6e27Sflorian 
199ae8c6e27Sflorian /** return nsec3 RR salt */
200ae8c6e27Sflorian static int
201ae8c6e27Sflorian nsec3_get_salt(struct ub_packed_rrset_key* rrset, int r,
202ae8c6e27Sflorian 	uint8_t** salt, size_t* saltlen)
203ae8c6e27Sflorian {
204ae8c6e27Sflorian         struct packed_rrset_data* d = (struct packed_rrset_data*)
205ae8c6e27Sflorian 	        rrset->entry.data;
206ae8c6e27Sflorian 	log_assert(d && r < (int)d->count);
207ae8c6e27Sflorian 	if(d->rr_len[r] < 2+5) {
208ae8c6e27Sflorian 		*salt = 0;
209ae8c6e27Sflorian 		*saltlen = 0;
210ae8c6e27Sflorian 		return 0; /* malformed */
211ae8c6e27Sflorian 	}
212ae8c6e27Sflorian 	*saltlen = (size_t)d->rr_data[r][2+4];
213ae8c6e27Sflorian 	if(d->rr_len[r] < 2+5+(size_t)*saltlen) {
214ae8c6e27Sflorian 		*salt = 0;
215ae8c6e27Sflorian 		*saltlen = 0;
216ae8c6e27Sflorian 		return 0; /* malformed */
217ae8c6e27Sflorian 	}
218ae8c6e27Sflorian 	*salt = d->rr_data[r]+2+5;
219ae8c6e27Sflorian 	return 1;
220ae8c6e27Sflorian }
221ae8c6e27Sflorian 
222ae8c6e27Sflorian int nsec3_get_params(struct ub_packed_rrset_key* rrset, int r,
223ae8c6e27Sflorian 	int* algo, size_t* iter, uint8_t** salt, size_t* saltlen)
224ae8c6e27Sflorian {
225ae8c6e27Sflorian 	if(!nsec3_known_algo(rrset, r) || nsec3_unknown_flags(rrset, r))
226ae8c6e27Sflorian 		return 0;
227ae8c6e27Sflorian 	if(!nsec3_get_salt(rrset, r, salt, saltlen))
228ae8c6e27Sflorian 		return 0;
229ae8c6e27Sflorian 	*algo = nsec3_get_algo(rrset, r);
230ae8c6e27Sflorian 	*iter = nsec3_get_iter(rrset, r);
231ae8c6e27Sflorian 	return 1;
232ae8c6e27Sflorian }
233ae8c6e27Sflorian 
234ae8c6e27Sflorian int
235ae8c6e27Sflorian nsec3_get_nextowner(struct ub_packed_rrset_key* rrset, int r,
236ae8c6e27Sflorian 	uint8_t** next, size_t* nextlen)
237ae8c6e27Sflorian {
238ae8c6e27Sflorian 	size_t saltlen;
239ae8c6e27Sflorian         struct packed_rrset_data* d = (struct packed_rrset_data*)
240ae8c6e27Sflorian 	        rrset->entry.data;
241ae8c6e27Sflorian 	log_assert(d && r < (int)d->count);
242ae8c6e27Sflorian 	if(d->rr_len[r] < 2+5) {
243ae8c6e27Sflorian 		*next = 0;
244ae8c6e27Sflorian 		*nextlen = 0;
245ae8c6e27Sflorian 		return 0; /* malformed */
246ae8c6e27Sflorian 	}
247ae8c6e27Sflorian 	saltlen = (size_t)d->rr_data[r][2+4];
248ae8c6e27Sflorian 	if(d->rr_len[r] < 2+5+saltlen+1) {
249ae8c6e27Sflorian 		*next = 0;
250ae8c6e27Sflorian 		*nextlen = 0;
251ae8c6e27Sflorian 		return 0; /* malformed */
252ae8c6e27Sflorian 	}
253ae8c6e27Sflorian 	*nextlen = (size_t)d->rr_data[r][2+5+saltlen];
254ae8c6e27Sflorian 	if(d->rr_len[r] < 2+5+saltlen+1+*nextlen) {
255ae8c6e27Sflorian 		*next = 0;
256ae8c6e27Sflorian 		*nextlen = 0;
257ae8c6e27Sflorian 		return 0; /* malformed */
258ae8c6e27Sflorian 	}
259ae8c6e27Sflorian 	*next = d->rr_data[r]+2+5+saltlen+1;
260ae8c6e27Sflorian 	return 1;
261ae8c6e27Sflorian }
262ae8c6e27Sflorian 
263ae8c6e27Sflorian size_t nsec3_hash_to_b32(uint8_t* hash, size_t hashlen, uint8_t* zone,
264ae8c6e27Sflorian 	size_t zonelen, uint8_t* buf, size_t max)
265ae8c6e27Sflorian {
266ae8c6e27Sflorian 	/* write b32 of name, leave one for length */
267ae8c6e27Sflorian 	int ret;
268ae8c6e27Sflorian 	if(max < hashlen*2+1) /* quick approx of b32, as if hexb16 */
269ae8c6e27Sflorian 		return 0;
270ae8c6e27Sflorian 	ret = sldns_b32_ntop_extended_hex(hash, hashlen, (char*)buf+1, max-1);
271ae8c6e27Sflorian 	if(ret < 1)
272ae8c6e27Sflorian 		return 0;
273ae8c6e27Sflorian 	buf[0] = (uint8_t)ret; /* length of b32 label */
274ae8c6e27Sflorian 	ret++;
275ae8c6e27Sflorian 	if(max - ret < zonelen)
276ae8c6e27Sflorian 		return 0;
277ae8c6e27Sflorian 	memmove(buf+ret, zone, zonelen);
278ae8c6e27Sflorian 	return zonelen+(size_t)ret;
279ae8c6e27Sflorian }
280ae8c6e27Sflorian 
281ae8c6e27Sflorian size_t nsec3_get_nextowner_b32(struct ub_packed_rrset_key* rrset, int r,
282ae8c6e27Sflorian 	uint8_t* buf, size_t max)
283ae8c6e27Sflorian {
284ae8c6e27Sflorian 	uint8_t* nm, *zone;
285ae8c6e27Sflorian 	size_t nmlen, zonelen;
286ae8c6e27Sflorian 	if(!nsec3_get_nextowner(rrset, r, &nm, &nmlen))
287ae8c6e27Sflorian 		return 0;
288ae8c6e27Sflorian 	/* append zone name; the owner name must be <b32>.zone */
289ae8c6e27Sflorian 	zone = rrset->rk.dname;
290ae8c6e27Sflorian 	zonelen = rrset->rk.dname_len;
291ae8c6e27Sflorian 	dname_remove_label(&zone, &zonelen);
292ae8c6e27Sflorian 	return nsec3_hash_to_b32(nm, nmlen, zone, zonelen, buf, max);
293ae8c6e27Sflorian }
294ae8c6e27Sflorian 
295ae8c6e27Sflorian int
296ae8c6e27Sflorian nsec3_has_type(struct ub_packed_rrset_key* rrset, int r, uint16_t type)
297ae8c6e27Sflorian {
298ae8c6e27Sflorian 	uint8_t* bitmap;
299ae8c6e27Sflorian 	size_t bitlen, skiplen;
300ae8c6e27Sflorian         struct packed_rrset_data* d = (struct packed_rrset_data*)
301ae8c6e27Sflorian 	        rrset->entry.data;
302ae8c6e27Sflorian 	log_assert(d && r < (int)d->count);
303ae8c6e27Sflorian 	skiplen = 2+4;
304ae8c6e27Sflorian 	/* skip salt */
305ae8c6e27Sflorian 	if(d->rr_len[r] < skiplen+1)
306ae8c6e27Sflorian 		return 0; /* malformed, too short */
307ae8c6e27Sflorian 	skiplen += 1+(size_t)d->rr_data[r][skiplen];
308ae8c6e27Sflorian 	/* skip next hashed owner */
309ae8c6e27Sflorian 	if(d->rr_len[r] < skiplen+1)
310ae8c6e27Sflorian 		return 0; /* malformed, too short */
311ae8c6e27Sflorian 	skiplen += 1+(size_t)d->rr_data[r][skiplen];
312ae8c6e27Sflorian 	if(d->rr_len[r] < skiplen)
313ae8c6e27Sflorian 		return 0; /* malformed, too short */
314ae8c6e27Sflorian 	bitlen = d->rr_len[r] - skiplen;
315ae8c6e27Sflorian 	bitmap = d->rr_data[r]+skiplen;
316ae8c6e27Sflorian 	return nsecbitmap_has_type_rdata(bitmap, bitlen, type);
317ae8c6e27Sflorian }
318ae8c6e27Sflorian 
319ae8c6e27Sflorian /**
320ae8c6e27Sflorian  * Iterate through NSEC3 list, per RR
321ae8c6e27Sflorian  * This routine gives the next RR in the list (or sets rrset null).
322ae8c6e27Sflorian  * Usage:
323ae8c6e27Sflorian  *
324ae8c6e27Sflorian  * size_t rrsetnum;
325ae8c6e27Sflorian  * int rrnum;
326ae8c6e27Sflorian  * struct ub_packed_rrset_key* rrset;
327ae8c6e27Sflorian  * for(rrset=filter_first(filter, &rrsetnum, &rrnum); rrset;
328ae8c6e27Sflorian  *	rrset=filter_next(filter, &rrsetnum, &rrnum))
329ae8c6e27Sflorian  *		do_stuff;
330ae8c6e27Sflorian  *
331ae8c6e27Sflorian  * Also filters out
332ae8c6e27Sflorian  * 	o unknown flag NSEC3s
333ae8c6e27Sflorian  * 	o unknown algorithm NSEC3s.
334ae8c6e27Sflorian  * @param filter: nsec3 filter structure.
335ae8c6e27Sflorian  * @param rrsetnum: in/out rrset number to look at.
336ae8c6e27Sflorian  * @param rrnum: in/out rr number in rrset to look at.
337ae8c6e27Sflorian  * @returns ptr to the next rrset (or NULL at end).
338ae8c6e27Sflorian  */
339ae8c6e27Sflorian static struct ub_packed_rrset_key*
340ae8c6e27Sflorian filter_next(struct nsec3_filter* filter, size_t* rrsetnum, int* rrnum)
341ae8c6e27Sflorian {
342ae8c6e27Sflorian 	size_t i;
343ae8c6e27Sflorian 	int r;
344ae8c6e27Sflorian 	uint8_t* nm;
345ae8c6e27Sflorian 	size_t nmlen;
346ae8c6e27Sflorian 	if(!filter->zone) /* empty list */
347ae8c6e27Sflorian 		return NULL;
348ae8c6e27Sflorian 	for(i=*rrsetnum; i<filter->num; i++) {
349ae8c6e27Sflorian 		/* see if RRset qualifies */
350ae8c6e27Sflorian 		if(ntohs(filter->list[i]->rk.type) != LDNS_RR_TYPE_NSEC3 ||
351ae8c6e27Sflorian 			ntohs(filter->list[i]->rk.rrset_class) !=
352ae8c6e27Sflorian 			filter->fclass)
353ae8c6e27Sflorian 			continue;
354ae8c6e27Sflorian 		/* check RRset zone */
355ae8c6e27Sflorian 		nm = filter->list[i]->rk.dname;
356ae8c6e27Sflorian 		nmlen = filter->list[i]->rk.dname_len;
357ae8c6e27Sflorian 		dname_remove_label(&nm, &nmlen);
358ae8c6e27Sflorian 		if(query_dname_compare(nm, filter->zone) != 0)
359ae8c6e27Sflorian 			continue;
360ae8c6e27Sflorian 		if(i == *rrsetnum)
361ae8c6e27Sflorian 			r = (*rrnum) + 1; /* continue at next RR */
362ae8c6e27Sflorian 		else	r = 0;		/* new RRset start at first RR */
363ae8c6e27Sflorian 		for(; r < (int)rrset_get_count(filter->list[i]); r++) {
364ae8c6e27Sflorian 			/* skip unknown flags, algo */
365ae8c6e27Sflorian 			if(nsec3_unknown_flags(filter->list[i], r) ||
366ae8c6e27Sflorian 				!nsec3_known_algo(filter->list[i], r))
367ae8c6e27Sflorian 				continue;
368ae8c6e27Sflorian 			/* this one is a good target */
369ae8c6e27Sflorian 			*rrsetnum = i;
370ae8c6e27Sflorian 			*rrnum = r;
371ae8c6e27Sflorian 			return filter->list[i];
372ae8c6e27Sflorian 		}
373ae8c6e27Sflorian 	}
374ae8c6e27Sflorian 	return NULL;
375ae8c6e27Sflorian }
376ae8c6e27Sflorian 
377ae8c6e27Sflorian /**
378ae8c6e27Sflorian  * Start iterating over NSEC3 records.
379ae8c6e27Sflorian  * @param filter: the filter structure, must have been filter_init-ed.
380ae8c6e27Sflorian  * @param rrsetnum: can be undefined on call, initialised.
381ae8c6e27Sflorian  * @param rrnum: can be undefined on call, initialised.
382ae8c6e27Sflorian  * @return first rrset of an NSEC3, together with rrnum this points to
383ae8c6e27Sflorian  *	the first RR to examine. Is NULL on empty list.
384ae8c6e27Sflorian  */
385ae8c6e27Sflorian static struct ub_packed_rrset_key*
386ae8c6e27Sflorian filter_first(struct nsec3_filter* filter, size_t* rrsetnum, int* rrnum)
387ae8c6e27Sflorian {
388ae8c6e27Sflorian 	*rrsetnum = 0;
389ae8c6e27Sflorian 	*rrnum = -1;
390ae8c6e27Sflorian 	return filter_next(filter, rrsetnum, rrnum);
391ae8c6e27Sflorian }
392ae8c6e27Sflorian 
393ae8c6e27Sflorian /** see if at least one RR is known (flags, algo) */
394ae8c6e27Sflorian static int
395ae8c6e27Sflorian nsec3_rrset_has_known(struct ub_packed_rrset_key* s)
396ae8c6e27Sflorian {
397ae8c6e27Sflorian 	int r;
398ae8c6e27Sflorian 	for(r=0; r < (int)rrset_get_count(s); r++) {
399ae8c6e27Sflorian 		if(!nsec3_unknown_flags(s, r) && nsec3_known_algo(s, r))
400ae8c6e27Sflorian 			return 1;
401ae8c6e27Sflorian 	}
402ae8c6e27Sflorian 	return 0;
403ae8c6e27Sflorian }
404ae8c6e27Sflorian 
405ae8c6e27Sflorian /**
406ae8c6e27Sflorian  * Initialize the filter structure.
407ae8c6e27Sflorian  * Finds the zone by looking at available NSEC3 records and best match.
408ae8c6e27Sflorian  * 	(skips the unknown flag and unknown algo NSEC3s).
409ae8c6e27Sflorian  *
410ae8c6e27Sflorian  * @param filter: nsec3 filter structure.
411ae8c6e27Sflorian  * @param list: list of rrsets, an array of them.
412ae8c6e27Sflorian  * @param num: number of rrsets in list.
413ae8c6e27Sflorian  * @param qinfo:
414ae8c6e27Sflorian  *	query name to match a zone for.
415ae8c6e27Sflorian  *	query type (if DS a higher zone must be chosen)
416ae8c6e27Sflorian  *	qclass, to filter NSEC3s with.
417ae8c6e27Sflorian  */
418ae8c6e27Sflorian static void
419ae8c6e27Sflorian filter_init(struct nsec3_filter* filter, struct ub_packed_rrset_key** list,
420ae8c6e27Sflorian 	size_t num, struct query_info* qinfo)
421ae8c6e27Sflorian {
422ae8c6e27Sflorian 	size_t i;
423ae8c6e27Sflorian 	uint8_t* nm;
424ae8c6e27Sflorian 	size_t nmlen;
425ae8c6e27Sflorian 	filter->zone = NULL;
426ae8c6e27Sflorian 	filter->zone_len = 0;
427ae8c6e27Sflorian 	filter->list = list;
428ae8c6e27Sflorian 	filter->num = num;
429ae8c6e27Sflorian 	filter->fclass = qinfo->qclass;
430ae8c6e27Sflorian 	for(i=0; i<num; i++) {
431ae8c6e27Sflorian 		/* ignore other stuff in the list */
432ae8c6e27Sflorian 		if(ntohs(list[i]->rk.type) != LDNS_RR_TYPE_NSEC3 ||
433ae8c6e27Sflorian 			ntohs(list[i]->rk.rrset_class) != qinfo->qclass)
434ae8c6e27Sflorian 			continue;
435ae8c6e27Sflorian 		/* skip unknown flags, algo */
436ae8c6e27Sflorian 		if(!nsec3_rrset_has_known(list[i]))
437ae8c6e27Sflorian 			continue;
438ae8c6e27Sflorian 
439ae8c6e27Sflorian 		/* since NSEC3s are base32.zonename, we can find the zone
440ae8c6e27Sflorian 		 * name by stripping off the first label of the record */
441ae8c6e27Sflorian 		nm = list[i]->rk.dname;
442ae8c6e27Sflorian 		nmlen = list[i]->rk.dname_len;
443ae8c6e27Sflorian 		dname_remove_label(&nm, &nmlen);
444ae8c6e27Sflorian 		/* if we find a domain that can prove about the qname,
445ae8c6e27Sflorian 		 * and if this domain is closer to the qname */
446ae8c6e27Sflorian 		if(dname_subdomain_c(qinfo->qname, nm) && (!filter->zone ||
447ae8c6e27Sflorian 			dname_subdomain_c(nm, filter->zone))) {
448ae8c6e27Sflorian 			/* for a type DS do not accept a zone equal to qname*/
449ae8c6e27Sflorian 			if(qinfo->qtype == LDNS_RR_TYPE_DS &&
450ae8c6e27Sflorian 				query_dname_compare(qinfo->qname, nm) == 0 &&
451ae8c6e27Sflorian 				!dname_is_root(qinfo->qname))
452ae8c6e27Sflorian 				continue;
453ae8c6e27Sflorian 			filter->zone = nm;
454ae8c6e27Sflorian 			filter->zone_len = nmlen;
455ae8c6e27Sflorian 		}
456ae8c6e27Sflorian 	}
457ae8c6e27Sflorian }
458ae8c6e27Sflorian 
459ae8c6e27Sflorian /**
460ae8c6e27Sflorian  * Find max iteration count using config settings and key size
461ae8c6e27Sflorian  * @param ve: validator environment with iteration count config settings.
462ae8c6e27Sflorian  * @param bits: key size
463ae8c6e27Sflorian  * @return max iteration count
464ae8c6e27Sflorian  */
465ae8c6e27Sflorian static size_t
466ae8c6e27Sflorian get_max_iter(struct val_env* ve, size_t bits)
467ae8c6e27Sflorian {
468ae8c6e27Sflorian 	int i;
469ae8c6e27Sflorian 	log_assert(ve->nsec3_keyiter_count > 0);
470ae8c6e27Sflorian 	/* round up to nearest config keysize, linear search, keep it small */
471ae8c6e27Sflorian 	for(i=0; i<ve->nsec3_keyiter_count; i++) {
472ae8c6e27Sflorian 		if(bits <= ve->nsec3_keysize[i])
473ae8c6e27Sflorian 			return ve->nsec3_maxiter[i];
474ae8c6e27Sflorian 	}
475ae8c6e27Sflorian 	/* else, use value for biggest key */
476ae8c6e27Sflorian 	return ve->nsec3_maxiter[ve->nsec3_keyiter_count-1];
477ae8c6e27Sflorian }
478ae8c6e27Sflorian 
479ae8c6e27Sflorian /**
480ae8c6e27Sflorian  * Determine if any of the NSEC3 rrs iteration count is too high, from key.
481ae8c6e27Sflorian  * @param ve: validator environment with iteration count config settings.
482ae8c6e27Sflorian  * @param filter: what NSEC3s to loop over.
483ae8c6e27Sflorian  * @param kkey: key entry used for verification; used for iteration counts.
484ae8c6e27Sflorian  * @return 1 if some nsec3s are above the max iteration count.
485ae8c6e27Sflorian  */
486ae8c6e27Sflorian static int
487ae8c6e27Sflorian nsec3_iteration_count_high(struct val_env* ve, struct nsec3_filter* filter,
488ae8c6e27Sflorian 	struct key_entry_key* kkey)
489ae8c6e27Sflorian {
490ae8c6e27Sflorian 	size_t rrsetnum;
491ae8c6e27Sflorian 	int rrnum;
492ae8c6e27Sflorian 	struct ub_packed_rrset_key* rrset;
493ae8c6e27Sflorian 	/* first determine the max number of iterations */
494ae8c6e27Sflorian 	size_t bits = key_entry_keysize(kkey);
495ae8c6e27Sflorian 	size_t max_iter = get_max_iter(ve, bits);
496ae8c6e27Sflorian 	verbose(VERB_ALGO, "nsec3: keysize %d bits, max iterations %d",
497ae8c6e27Sflorian 		(int)bits, (int)max_iter);
498ae8c6e27Sflorian 
499ae8c6e27Sflorian 	for(rrset=filter_first(filter, &rrsetnum, &rrnum); rrset;
500ae8c6e27Sflorian 		rrset=filter_next(filter, &rrsetnum, &rrnum)) {
501ae8c6e27Sflorian 		if(nsec3_get_iter(rrset, rrnum) > max_iter)
502ae8c6e27Sflorian 			return 1;
503ae8c6e27Sflorian 	}
504ae8c6e27Sflorian 	return 0;
505ae8c6e27Sflorian }
506ae8c6e27Sflorian 
507ae8c6e27Sflorian /* nsec3_cache_compare for rbtree */
508ae8c6e27Sflorian int
509ae8c6e27Sflorian nsec3_hash_cmp(const void* c1, const void* c2)
510ae8c6e27Sflorian {
511ae8c6e27Sflorian 	struct nsec3_cached_hash* h1 = (struct nsec3_cached_hash*)c1;
512ae8c6e27Sflorian 	struct nsec3_cached_hash* h2 = (struct nsec3_cached_hash*)c2;
513ae8c6e27Sflorian 	uint8_t* s1, *s2;
514ae8c6e27Sflorian 	size_t s1len, s2len;
515ae8c6e27Sflorian 	int c = query_dname_compare(h1->dname, h2->dname);
516ae8c6e27Sflorian 	if(c != 0)
517ae8c6e27Sflorian 		return c;
518ae8c6e27Sflorian 	/* compare parameters */
519ae8c6e27Sflorian 	/* if both malformed, its equal, robustness */
520ae8c6e27Sflorian 	if(nsec3_get_algo(h1->nsec3, h1->rr) !=
521ae8c6e27Sflorian 		nsec3_get_algo(h2->nsec3, h2->rr)) {
522ae8c6e27Sflorian 		if(nsec3_get_algo(h1->nsec3, h1->rr) <
523ae8c6e27Sflorian 			nsec3_get_algo(h2->nsec3, h2->rr))
524ae8c6e27Sflorian 			return -1;
525ae8c6e27Sflorian 		return 1;
526ae8c6e27Sflorian 	}
527ae8c6e27Sflorian 	if(nsec3_get_iter(h1->nsec3, h1->rr) !=
528ae8c6e27Sflorian 		nsec3_get_iter(h2->nsec3, h2->rr)) {
529ae8c6e27Sflorian 		if(nsec3_get_iter(h1->nsec3, h1->rr) <
530ae8c6e27Sflorian 			nsec3_get_iter(h2->nsec3, h2->rr))
531ae8c6e27Sflorian 			return -1;
532ae8c6e27Sflorian 		return 1;
533ae8c6e27Sflorian 	}
534ae8c6e27Sflorian 	(void)nsec3_get_salt(h1->nsec3, h1->rr, &s1, &s1len);
535ae8c6e27Sflorian 	(void)nsec3_get_salt(h2->nsec3, h2->rr, &s2, &s2len);
536ae8c6e27Sflorian 	if(s1len == 0 && s2len == 0)
537ae8c6e27Sflorian 		return 0;
538ae8c6e27Sflorian 	if(!s1) return -1;
539ae8c6e27Sflorian 	if(!s2) return 1;
540ae8c6e27Sflorian 	if(s1len != s2len) {
541ae8c6e27Sflorian 		if(s1len < s2len)
542ae8c6e27Sflorian 			return -1;
543ae8c6e27Sflorian 		return 1;
544ae8c6e27Sflorian 	}
545ae8c6e27Sflorian 	return memcmp(s1, s2, s1len);
546ae8c6e27Sflorian }
547ae8c6e27Sflorian 
548fed3efa7Sflorian int
549fed3efa7Sflorian nsec3_cache_table_init(struct nsec3_cache_table* ct, struct regional* region)
550fed3efa7Sflorian {
551fed3efa7Sflorian 	if(ct->ct) return 1;
552fed3efa7Sflorian 	ct->ct = (rbtree_type*)regional_alloc(region, sizeof(*ct->ct));
553fed3efa7Sflorian 	if(!ct->ct) return 0;
554fed3efa7Sflorian 	ct->region = region;
555fed3efa7Sflorian 	rbtree_init(ct->ct, &nsec3_hash_cmp);
556fed3efa7Sflorian 	return 1;
557fed3efa7Sflorian }
558fed3efa7Sflorian 
559ae8c6e27Sflorian size_t
560ae8c6e27Sflorian nsec3_get_hashed(sldns_buffer* buf, uint8_t* nm, size_t nmlen, int algo,
561ae8c6e27Sflorian 	size_t iter, uint8_t* salt, size_t saltlen, uint8_t* res, size_t max)
562ae8c6e27Sflorian {
563ae8c6e27Sflorian 	size_t i, hash_len;
564ae8c6e27Sflorian 	/* prepare buffer for first iteration */
565ae8c6e27Sflorian 	sldns_buffer_clear(buf);
566ae8c6e27Sflorian 	sldns_buffer_write(buf, nm, nmlen);
567ae8c6e27Sflorian 	query_dname_tolower(sldns_buffer_begin(buf));
568ae8c6e27Sflorian 	sldns_buffer_write(buf, salt, saltlen);
569ae8c6e27Sflorian 	sldns_buffer_flip(buf);
570ae8c6e27Sflorian 	hash_len = nsec3_hash_algo_size_supported(algo);
571ae8c6e27Sflorian 	if(hash_len == 0) {
572ae8c6e27Sflorian 		log_err("nsec3 hash of unknown algo %d", algo);
573ae8c6e27Sflorian 		return 0;
574ae8c6e27Sflorian 	}
575ae8c6e27Sflorian 	if(hash_len > max)
576ae8c6e27Sflorian 		return 0;
577ae8c6e27Sflorian 	if(!secalgo_nsec3_hash(algo, (unsigned char*)sldns_buffer_begin(buf),
578ae8c6e27Sflorian 		sldns_buffer_limit(buf), (unsigned char*)res))
579ae8c6e27Sflorian 		return 0;
580ae8c6e27Sflorian 	for(i=0; i<iter; i++) {
581ae8c6e27Sflorian 		sldns_buffer_clear(buf);
582ae8c6e27Sflorian 		sldns_buffer_write(buf, res, hash_len);
583ae8c6e27Sflorian 		sldns_buffer_write(buf, salt, saltlen);
584ae8c6e27Sflorian 		sldns_buffer_flip(buf);
585ae8c6e27Sflorian 		if(!secalgo_nsec3_hash(algo,
586ae8c6e27Sflorian 			(unsigned char*)sldns_buffer_begin(buf),
587ae8c6e27Sflorian 			sldns_buffer_limit(buf), (unsigned char*)res))
588ae8c6e27Sflorian 			return 0;
589ae8c6e27Sflorian 	}
590ae8c6e27Sflorian 	return hash_len;
591ae8c6e27Sflorian }
592ae8c6e27Sflorian 
593ae8c6e27Sflorian /** perform hash of name */
594ae8c6e27Sflorian static int
595ae8c6e27Sflorian nsec3_calc_hash(struct regional* region, sldns_buffer* buf,
596ae8c6e27Sflorian 	struct nsec3_cached_hash* c)
597ae8c6e27Sflorian {
598ae8c6e27Sflorian 	int algo = nsec3_get_algo(c->nsec3, c->rr);
599ae8c6e27Sflorian 	size_t iter = nsec3_get_iter(c->nsec3, c->rr);
600ae8c6e27Sflorian 	uint8_t* salt;
601ae8c6e27Sflorian 	size_t saltlen, i;
602ae8c6e27Sflorian 	if(!nsec3_get_salt(c->nsec3, c->rr, &salt, &saltlen))
603ae8c6e27Sflorian 		return -1;
604ae8c6e27Sflorian 	/* prepare buffer for first iteration */
605ae8c6e27Sflorian 	sldns_buffer_clear(buf);
606ae8c6e27Sflorian 	sldns_buffer_write(buf, c->dname, c->dname_len);
607ae8c6e27Sflorian 	query_dname_tolower(sldns_buffer_begin(buf));
608ae8c6e27Sflorian 	sldns_buffer_write(buf, salt, saltlen);
609ae8c6e27Sflorian 	sldns_buffer_flip(buf);
610ae8c6e27Sflorian 	c->hash_len = nsec3_hash_algo_size_supported(algo);
611ae8c6e27Sflorian 	if(c->hash_len == 0) {
612ae8c6e27Sflorian 		log_err("nsec3 hash of unknown algo %d", algo);
613ae8c6e27Sflorian 		return -1;
614ae8c6e27Sflorian 	}
615ae8c6e27Sflorian 	c->hash = (uint8_t*)regional_alloc(region, c->hash_len);
616ae8c6e27Sflorian 	if(!c->hash)
617ae8c6e27Sflorian 		return 0;
618ae8c6e27Sflorian 	(void)secalgo_nsec3_hash(algo, (unsigned char*)sldns_buffer_begin(buf),
619ae8c6e27Sflorian 		sldns_buffer_limit(buf), (unsigned char*)c->hash);
620ae8c6e27Sflorian 	for(i=0; i<iter; i++) {
621ae8c6e27Sflorian 		sldns_buffer_clear(buf);
622ae8c6e27Sflorian 		sldns_buffer_write(buf, c->hash, c->hash_len);
623ae8c6e27Sflorian 		sldns_buffer_write(buf, salt, saltlen);
624ae8c6e27Sflorian 		sldns_buffer_flip(buf);
625ae8c6e27Sflorian 		(void)secalgo_nsec3_hash(algo,
626ae8c6e27Sflorian 			(unsigned char*)sldns_buffer_begin(buf),
627ae8c6e27Sflorian 			sldns_buffer_limit(buf), (unsigned char*)c->hash);
628ae8c6e27Sflorian 	}
629ae8c6e27Sflorian 	return 1;
630ae8c6e27Sflorian }
631ae8c6e27Sflorian 
632ae8c6e27Sflorian /** perform b32 encoding of hash */
633ae8c6e27Sflorian static int
634ae8c6e27Sflorian nsec3_calc_b32(struct regional* region, sldns_buffer* buf,
635ae8c6e27Sflorian 	struct nsec3_cached_hash* c)
636ae8c6e27Sflorian {
637ae8c6e27Sflorian 	int r;
638ae8c6e27Sflorian 	sldns_buffer_clear(buf);
639ae8c6e27Sflorian 	r = sldns_b32_ntop_extended_hex(c->hash, c->hash_len,
640ae8c6e27Sflorian 		(char*)sldns_buffer_begin(buf), sldns_buffer_limit(buf));
641ae8c6e27Sflorian 	if(r < 1) {
642ae8c6e27Sflorian 		log_err("b32_ntop_extended_hex: error in encoding: %d", r);
643ae8c6e27Sflorian 		return 0;
644ae8c6e27Sflorian 	}
645ae8c6e27Sflorian 	c->b32_len = (size_t)r;
646ae8c6e27Sflorian 	c->b32 = regional_alloc_init(region, sldns_buffer_begin(buf),
647ae8c6e27Sflorian 		c->b32_len);
648ae8c6e27Sflorian 	if(!c->b32)
649ae8c6e27Sflorian 		return 0;
650ae8c6e27Sflorian 	return 1;
651ae8c6e27Sflorian }
652ae8c6e27Sflorian 
653ae8c6e27Sflorian int
654ae8c6e27Sflorian nsec3_hash_name(rbtree_type* table, struct regional* region, sldns_buffer* buf,
655ae8c6e27Sflorian 	struct ub_packed_rrset_key* nsec3, int rr, uint8_t* dname,
656ae8c6e27Sflorian 	size_t dname_len, struct nsec3_cached_hash** hash)
657ae8c6e27Sflorian {
658ae8c6e27Sflorian 	struct nsec3_cached_hash* c;
659ae8c6e27Sflorian 	struct nsec3_cached_hash looki;
660ae8c6e27Sflorian #ifdef UNBOUND_DEBUG
661ae8c6e27Sflorian 	rbnode_type* n;
662ae8c6e27Sflorian #endif
663ae8c6e27Sflorian 	int r;
664ae8c6e27Sflorian 	looki.node.key = &looki;
665ae8c6e27Sflorian 	looki.nsec3 = nsec3;
666ae8c6e27Sflorian 	looki.rr = rr;
667ae8c6e27Sflorian 	looki.dname = dname;
668ae8c6e27Sflorian 	looki.dname_len = dname_len;
669ae8c6e27Sflorian 	/* lookup first in cache */
670ae8c6e27Sflorian 	c = (struct nsec3_cached_hash*)rbtree_search(table, &looki);
671ae8c6e27Sflorian 	if(c) {
672ae8c6e27Sflorian 		*hash = c;
673fed3efa7Sflorian 		return 2;
674ae8c6e27Sflorian 	}
675ae8c6e27Sflorian 	/* create a new entry */
676ae8c6e27Sflorian 	c = (struct nsec3_cached_hash*)regional_alloc(region, sizeof(*c));
677ae8c6e27Sflorian 	if(!c) return 0;
678ae8c6e27Sflorian 	c->node.key = c;
679ae8c6e27Sflorian 	c->nsec3 = nsec3;
680ae8c6e27Sflorian 	c->rr = rr;
681ae8c6e27Sflorian 	c->dname = dname;
682ae8c6e27Sflorian 	c->dname_len = dname_len;
683ae8c6e27Sflorian 	r = nsec3_calc_hash(region, buf, c);
684ae8c6e27Sflorian 	if(r != 1)
685fed3efa7Sflorian 		return r;  /* returns -1 or 0 */
686ae8c6e27Sflorian 	r = nsec3_calc_b32(region, buf, c);
687ae8c6e27Sflorian 	if(r != 1)
688fed3efa7Sflorian 		return r;  /* returns 0 */
689ae8c6e27Sflorian #ifdef UNBOUND_DEBUG
690ae8c6e27Sflorian 	n =
691ae8c6e27Sflorian #else
692ae8c6e27Sflorian 	(void)
693ae8c6e27Sflorian #endif
694ae8c6e27Sflorian 	rbtree_insert(table, &c->node);
695ae8c6e27Sflorian 	log_assert(n); /* cannot be duplicate, just did lookup */
696ae8c6e27Sflorian 	*hash = c;
697ae8c6e27Sflorian 	return 1;
698ae8c6e27Sflorian }
699ae8c6e27Sflorian 
700ae8c6e27Sflorian /**
701ae8c6e27Sflorian  * compare a label lowercased
702ae8c6e27Sflorian  */
703ae8c6e27Sflorian static int
704ae8c6e27Sflorian label_compare_lower(uint8_t* lab1, uint8_t* lab2, size_t lablen)
705ae8c6e27Sflorian {
706ae8c6e27Sflorian 	size_t i;
707ae8c6e27Sflorian 	for(i=0; i<lablen; i++) {
708ae8c6e27Sflorian 		if(tolower((unsigned char)*lab1) != tolower((unsigned char)*lab2)) {
709ae8c6e27Sflorian 			if(tolower((unsigned char)*lab1) < tolower((unsigned char)*lab2))
710ae8c6e27Sflorian 				return -1;
711ae8c6e27Sflorian 			return 1;
712ae8c6e27Sflorian 		}
713ae8c6e27Sflorian 		lab1++;
714ae8c6e27Sflorian 		lab2++;
715ae8c6e27Sflorian 	}
716ae8c6e27Sflorian 	return 0;
717ae8c6e27Sflorian }
718ae8c6e27Sflorian 
719ae8c6e27Sflorian /**
720ae8c6e27Sflorian  * Compare a hashed name with the owner name of an NSEC3 RRset.
721ae8c6e27Sflorian  * @param flt: filter with zone name.
722ae8c6e27Sflorian  * @param hash: the hashed name.
723ae8c6e27Sflorian  * @param s: rrset with owner name.
724ae8c6e27Sflorian  * @return true if matches exactly, false if not.
725ae8c6e27Sflorian  */
726ae8c6e27Sflorian static int
727ae8c6e27Sflorian nsec3_hash_matches_owner(struct nsec3_filter* flt,
728ae8c6e27Sflorian 	struct nsec3_cached_hash* hash, struct ub_packed_rrset_key* s)
729ae8c6e27Sflorian {
730ae8c6e27Sflorian 	uint8_t* nm = s->rk.dname;
731fed3efa7Sflorian 	if(!hash) return 0; /* please clang */
732ae8c6e27Sflorian 	/* compare, does hash of name based on params in this NSEC3
733ae8c6e27Sflorian 	 * match the owner name of this NSEC3?
734ae8c6e27Sflorian 	 * name must be: <hashlength>base32 . zone name
735ae8c6e27Sflorian 	 * so; first label must not be root label (not zero length),
736ae8c6e27Sflorian 	 * and match the b32 encoded hash length,
737ae8c6e27Sflorian 	 * and the label content match the b32 encoded hash
738ae8c6e27Sflorian 	 * and the rest must be the zone name.
739ae8c6e27Sflorian 	 */
740ae8c6e27Sflorian 	if(hash->b32_len != 0 && (size_t)nm[0] == hash->b32_len &&
741ae8c6e27Sflorian 		label_compare_lower(nm+1, hash->b32, hash->b32_len) == 0 &&
742ae8c6e27Sflorian 		query_dname_compare(nm+(size_t)nm[0]+1, flt->zone) == 0) {
743ae8c6e27Sflorian 		return 1;
744ae8c6e27Sflorian 	}
745ae8c6e27Sflorian 	return 0;
746ae8c6e27Sflorian }
747ae8c6e27Sflorian 
748ae8c6e27Sflorian /**
749ae8c6e27Sflorian  * Find matching NSEC3
750ae8c6e27Sflorian  * Find the NSEC3Record that matches a hash of a name.
751ae8c6e27Sflorian  * @param env: module environment with temporary region and buffer.
752ae8c6e27Sflorian  * @param flt: the NSEC3 RR filter, contains zone name and RRs.
753ae8c6e27Sflorian  * @param ct: cached hashes table.
754ae8c6e27Sflorian  * @param nm: name to look for.
755ae8c6e27Sflorian  * @param nmlen: length of name.
756ae8c6e27Sflorian  * @param rrset: nsec3 that matches is returned here.
757ae8c6e27Sflorian  * @param rr: rr number in nsec3 rrset that matches.
758fed3efa7Sflorian  * @param calculations: current hash calculations.
759ae8c6e27Sflorian  * @return true if a matching NSEC3 is found, false if not.
760ae8c6e27Sflorian  */
761ae8c6e27Sflorian static int
762ae8c6e27Sflorian find_matching_nsec3(struct module_env* env, struct nsec3_filter* flt,
763fed3efa7Sflorian 	struct nsec3_cache_table* ct, uint8_t* nm, size_t nmlen,
764fed3efa7Sflorian 	struct ub_packed_rrset_key** rrset, int* rr,
765fed3efa7Sflorian 	int* calculations)
766ae8c6e27Sflorian {
767ae8c6e27Sflorian 	size_t i_rs;
768ae8c6e27Sflorian 	int i_rr;
769ae8c6e27Sflorian 	struct ub_packed_rrset_key* s;
770ae8c6e27Sflorian 	struct nsec3_cached_hash* hash = NULL;
771ae8c6e27Sflorian 	int r;
772fed3efa7Sflorian 	int calc_errors = 0;
773ae8c6e27Sflorian 
774ae8c6e27Sflorian 	/* this loop skips other-zone and unknown NSEC3s, also non-NSEC3 RRs */
775ae8c6e27Sflorian 	for(s=filter_first(flt, &i_rs, &i_rr); s;
776ae8c6e27Sflorian 		s=filter_next(flt, &i_rs, &i_rr)) {
777fed3efa7Sflorian 		/* check if we are allowed more calculations */
778fed3efa7Sflorian 		if(*calculations >= MAX_NSEC3_CALCULATIONS) {
779fed3efa7Sflorian 			if(calc_errors == *calculations) {
780fed3efa7Sflorian 				*calculations = MAX_NSEC3_ERRORS;
781fed3efa7Sflorian 			}
782fed3efa7Sflorian 			break;
783fed3efa7Sflorian 		}
784ae8c6e27Sflorian 		/* get name hashed for this NSEC3 RR */
785fed3efa7Sflorian 		r = nsec3_hash_name(ct->ct, ct->region, env->scratch_buffer,
786ae8c6e27Sflorian 			s, i_rr, nm, nmlen, &hash);
787ae8c6e27Sflorian 		if(r == 0) {
788ae8c6e27Sflorian 			log_err("nsec3: malloc failure");
789ae8c6e27Sflorian 			break; /* alloc failure */
790fed3efa7Sflorian 		} else if(r < 0) {
791fed3efa7Sflorian 			/* malformed NSEC3 */
792fed3efa7Sflorian 			calc_errors++;
793fed3efa7Sflorian 			(*calculations)++;
794fed3efa7Sflorian 			continue;
795fed3efa7Sflorian 		} else {
796fed3efa7Sflorian 			if(r == 1) (*calculations)++;
797fed3efa7Sflorian 			if(nsec3_hash_matches_owner(flt, hash, s)) {
798ae8c6e27Sflorian 				*rrset = s; /* rrset with this name */
799ae8c6e27Sflorian 				*rr = i_rr; /* matches hash with these parameters */
800ae8c6e27Sflorian 				return 1;
801ae8c6e27Sflorian 			}
802ae8c6e27Sflorian 		}
803fed3efa7Sflorian 	}
804ae8c6e27Sflorian 	*rrset = NULL;
805ae8c6e27Sflorian 	*rr = 0;
806ae8c6e27Sflorian 	return 0;
807ae8c6e27Sflorian }
808ae8c6e27Sflorian 
809ae8c6e27Sflorian int
810ae8c6e27Sflorian nsec3_covers(uint8_t* zone, struct nsec3_cached_hash* hash,
811ae8c6e27Sflorian 	struct ub_packed_rrset_key* rrset, int rr, sldns_buffer* buf)
812ae8c6e27Sflorian {
813ae8c6e27Sflorian 	uint8_t* next, *owner;
814ae8c6e27Sflorian 	size_t nextlen;
815ae8c6e27Sflorian 	int len;
816ae8c6e27Sflorian 	if(!nsec3_get_nextowner(rrset, rr, &next, &nextlen))
817ae8c6e27Sflorian 		return 0; /* malformed RR proves nothing */
818ae8c6e27Sflorian 
819fed3efa7Sflorian 	if(!hash) return 0; /* please clang */
820ae8c6e27Sflorian 	/* check the owner name is a hashed value . apex
821ae8c6e27Sflorian 	 * base32 encoded values must have equal length.
822ae8c6e27Sflorian 	 * hash_value and next hash value must have equal length. */
823ae8c6e27Sflorian 	if(nextlen != hash->hash_len || hash->hash_len==0||hash->b32_len==0||
824ae8c6e27Sflorian 		(size_t)*rrset->rk.dname != hash->b32_len ||
825ae8c6e27Sflorian 		query_dname_compare(rrset->rk.dname+1+
826ae8c6e27Sflorian 			(size_t)*rrset->rk.dname, zone) != 0)
827ae8c6e27Sflorian 		return 0; /* bad lengths or owner name */
828ae8c6e27Sflorian 
829ae8c6e27Sflorian 	/* This is the "normal case: owner < next and owner < hash < next */
830ae8c6e27Sflorian 	if(label_compare_lower(rrset->rk.dname+1, hash->b32,
831ae8c6e27Sflorian 		hash->b32_len) < 0 &&
832ae8c6e27Sflorian 		memcmp(hash->hash, next, nextlen) < 0)
833ae8c6e27Sflorian 		return 1;
834ae8c6e27Sflorian 
835ae8c6e27Sflorian 	/* convert owner name from text to binary */
836ae8c6e27Sflorian 	sldns_buffer_clear(buf);
837ae8c6e27Sflorian 	owner = sldns_buffer_begin(buf);
838ae8c6e27Sflorian 	len = sldns_b32_pton_extended_hex((char*)rrset->rk.dname+1,
839ae8c6e27Sflorian 		hash->b32_len, owner, sldns_buffer_limit(buf));
840ae8c6e27Sflorian 	if(len<1)
841ae8c6e27Sflorian 		return 0; /* bad owner name in some way */
842ae8c6e27Sflorian 	if((size_t)len != hash->hash_len || (size_t)len != nextlen)
843ae8c6e27Sflorian 		return 0; /* wrong length */
844ae8c6e27Sflorian 
845ae8c6e27Sflorian 	/* this is the end of zone case: next <= owner &&
846ae8c6e27Sflorian 	 * 	(hash > owner || hash < next)
847ae8c6e27Sflorian 	 * this also covers the only-apex case of next==owner.
848ae8c6e27Sflorian 	 */
849ae8c6e27Sflorian 	if(memcmp(next, owner, nextlen) <= 0 &&
850ae8c6e27Sflorian 		( memcmp(hash->hash, owner, nextlen) > 0 ||
851ae8c6e27Sflorian 		  memcmp(hash->hash, next, nextlen) < 0)) {
852ae8c6e27Sflorian 		return 1;
853ae8c6e27Sflorian 	}
854ae8c6e27Sflorian 	return 0;
855ae8c6e27Sflorian }
856ae8c6e27Sflorian 
857ae8c6e27Sflorian /**
858ae8c6e27Sflorian  * findCoveringNSEC3
859ae8c6e27Sflorian  * Given a name, find a covering NSEC3 from among a list of NSEC3s.
860ae8c6e27Sflorian  *
861ae8c6e27Sflorian  * @param env: module environment with temporary region and buffer.
862ae8c6e27Sflorian  * @param flt: the NSEC3 RR filter, contains zone name and RRs.
863ae8c6e27Sflorian  * @param ct: cached hashes table.
864ae8c6e27Sflorian  * @param nm: name to check if covered.
865ae8c6e27Sflorian  * @param nmlen: length of name.
866ae8c6e27Sflorian  * @param rrset: covering NSEC3 rrset is returned here.
867ae8c6e27Sflorian  * @param rr: rr of cover is returned here.
868fed3efa7Sflorian  * @param calculations: current hash calculations.
869ae8c6e27Sflorian  * @return true if a covering NSEC3 is found, false if not.
870ae8c6e27Sflorian  */
871ae8c6e27Sflorian static int
872ae8c6e27Sflorian find_covering_nsec3(struct module_env* env, struct nsec3_filter* flt,
873fed3efa7Sflorian 	struct nsec3_cache_table* ct, uint8_t* nm, size_t nmlen,
874fed3efa7Sflorian 	struct ub_packed_rrset_key** rrset, int* rr,
875fed3efa7Sflorian 	int* calculations)
876ae8c6e27Sflorian {
877ae8c6e27Sflorian 	size_t i_rs;
878ae8c6e27Sflorian 	int i_rr;
879ae8c6e27Sflorian 	struct ub_packed_rrset_key* s;
880ae8c6e27Sflorian 	struct nsec3_cached_hash* hash = NULL;
881ae8c6e27Sflorian 	int r;
882fed3efa7Sflorian 	int calc_errors = 0;
883ae8c6e27Sflorian 
884ae8c6e27Sflorian 	/* this loop skips other-zone and unknown NSEC3s, also non-NSEC3 RRs */
885ae8c6e27Sflorian 	for(s=filter_first(flt, &i_rs, &i_rr); s;
886ae8c6e27Sflorian 		s=filter_next(flt, &i_rs, &i_rr)) {
887fed3efa7Sflorian 		/* check if we are allowed more calculations */
888fed3efa7Sflorian 		if(*calculations >= MAX_NSEC3_CALCULATIONS) {
889fed3efa7Sflorian 			if(calc_errors == *calculations) {
890fed3efa7Sflorian 				*calculations = MAX_NSEC3_ERRORS;
891fed3efa7Sflorian 			}
892fed3efa7Sflorian 			break;
893fed3efa7Sflorian 		}
894ae8c6e27Sflorian 		/* get name hashed for this NSEC3 RR */
895fed3efa7Sflorian 		r = nsec3_hash_name(ct->ct, ct->region, env->scratch_buffer,
896ae8c6e27Sflorian 			s, i_rr, nm, nmlen, &hash);
897ae8c6e27Sflorian 		if(r == 0) {
898ae8c6e27Sflorian 			log_err("nsec3: malloc failure");
899ae8c6e27Sflorian 			break; /* alloc failure */
900fed3efa7Sflorian 		} else if(r < 0) {
901fed3efa7Sflorian 			/* malformed NSEC3 */
902fed3efa7Sflorian 			calc_errors++;
903fed3efa7Sflorian 			(*calculations)++;
904fed3efa7Sflorian 			continue;
905fed3efa7Sflorian 		} else {
906fed3efa7Sflorian 			if(r == 1) (*calculations)++;
907fed3efa7Sflorian 			if(nsec3_covers(flt->zone, hash, s, i_rr,
908ae8c6e27Sflorian 				env->scratch_buffer)) {
909ae8c6e27Sflorian 				*rrset = s; /* rrset with this name */
910ae8c6e27Sflorian 				*rr = i_rr; /* covers hash with these parameters */
911ae8c6e27Sflorian 				return 1;
912ae8c6e27Sflorian 			}
913ae8c6e27Sflorian 		}
914fed3efa7Sflorian 	}
915ae8c6e27Sflorian 	*rrset = NULL;
916ae8c6e27Sflorian 	*rr = 0;
917ae8c6e27Sflorian 	return 0;
918ae8c6e27Sflorian }
919ae8c6e27Sflorian 
920ae8c6e27Sflorian /**
921ae8c6e27Sflorian  * findClosestEncloser
922ae8c6e27Sflorian  * Given a name and a list of NSEC3s, find the candidate closest encloser.
923ae8c6e27Sflorian  * This will be the first ancestor of 'name' (including itself) to have a
924ae8c6e27Sflorian  * matching NSEC3 RR.
925ae8c6e27Sflorian  * @param env: module environment with temporary region and buffer.
926ae8c6e27Sflorian  * @param flt: the NSEC3 RR filter, contains zone name and RRs.
927ae8c6e27Sflorian  * @param ct: cached hashes table.
928ae8c6e27Sflorian  * @param qinfo: query that is verified for.
929ae8c6e27Sflorian  * @param ce: closest encloser information is returned in here.
930fed3efa7Sflorian  * @param calculations: current hash calculations.
931ae8c6e27Sflorian  * @return true if a closest encloser candidate is found, false if not.
932ae8c6e27Sflorian  */
933ae8c6e27Sflorian static int
934ae8c6e27Sflorian nsec3_find_closest_encloser(struct module_env* env, struct nsec3_filter* flt,
935fed3efa7Sflorian 	struct nsec3_cache_table* ct, struct query_info* qinfo,
936fed3efa7Sflorian 	struct ce_response* ce, int* calculations)
937ae8c6e27Sflorian {
938ae8c6e27Sflorian 	uint8_t* nm = qinfo->qname;
939ae8c6e27Sflorian 	size_t nmlen = qinfo->qname_len;
940ae8c6e27Sflorian 
941ae8c6e27Sflorian 	/* This scans from longest name to shortest, so the first match
942ae8c6e27Sflorian 	 * we find is the only viable candidate. */
943ae8c6e27Sflorian 
944ae8c6e27Sflorian 	/* (David:) FIXME: modify so that the NSEC3 matching the zone apex need
945ae8c6e27Sflorian 	 * not be present. (Mark Andrews idea).
946ae8c6e27Sflorian 	 * (Wouter:) But make sure you check for DNAME bit in zone apex,
947ae8c6e27Sflorian 	 * if the NSEC3 you find is the only NSEC3 in the zone, then this
948ae8c6e27Sflorian 	 * may be the case. */
949ae8c6e27Sflorian 
950ae8c6e27Sflorian 	while(dname_subdomain_c(nm, flt->zone)) {
951fed3efa7Sflorian 		if(*calculations >= MAX_NSEC3_CALCULATIONS ||
952fed3efa7Sflorian 			*calculations == MAX_NSEC3_ERRORS) {
953fed3efa7Sflorian 			return 0;
954fed3efa7Sflorian 		}
955ae8c6e27Sflorian 		if(find_matching_nsec3(env, flt, ct, nm, nmlen,
956fed3efa7Sflorian 			&ce->ce_rrset, &ce->ce_rr, calculations)) {
957ae8c6e27Sflorian 			ce->ce = nm;
958ae8c6e27Sflorian 			ce->ce_len = nmlen;
959ae8c6e27Sflorian 			return 1;
960ae8c6e27Sflorian 		}
961ae8c6e27Sflorian 		dname_remove_label(&nm, &nmlen);
962ae8c6e27Sflorian 	}
963ae8c6e27Sflorian 	return 0;
964ae8c6e27Sflorian }
965ae8c6e27Sflorian 
966ae8c6e27Sflorian /**
967ae8c6e27Sflorian  * Given a qname and its proven closest encloser, calculate the "next
968ae8c6e27Sflorian  * closest" name. Basically, this is the name that is one label longer than
969ae8c6e27Sflorian  * the closest encloser that is still a subdomain of qname.
970ae8c6e27Sflorian  *
971ae8c6e27Sflorian  * @param qname: query name.
972ae8c6e27Sflorian  * @param qnamelen: length of qname.
973ae8c6e27Sflorian  * @param ce: closest encloser
974ae8c6e27Sflorian  * @param nm: result name.
975ae8c6e27Sflorian  * @param nmlen: length of nm.
976ae8c6e27Sflorian  */
977ae8c6e27Sflorian static void
978ae8c6e27Sflorian next_closer(uint8_t* qname, size_t qnamelen, uint8_t* ce,
979ae8c6e27Sflorian 	uint8_t** nm, size_t* nmlen)
980ae8c6e27Sflorian {
981ae8c6e27Sflorian 	int strip = dname_count_labels(qname) - dname_count_labels(ce) -1;
982ae8c6e27Sflorian 	*nm = qname;
983ae8c6e27Sflorian 	*nmlen = qnamelen;
984ae8c6e27Sflorian 	if(strip>0)
985ae8c6e27Sflorian 		dname_remove_labels(nm, nmlen, strip);
986ae8c6e27Sflorian }
987ae8c6e27Sflorian 
988ae8c6e27Sflorian /**
989ae8c6e27Sflorian  * proveClosestEncloser
990ae8c6e27Sflorian  * Given a List of nsec3 RRs, find and prove the closest encloser to qname.
991ae8c6e27Sflorian  * @param env: module environment with temporary region and buffer.
992ae8c6e27Sflorian  * @param flt: the NSEC3 RR filter, contains zone name and RRs.
993ae8c6e27Sflorian  * @param ct: cached hashes table.
994ae8c6e27Sflorian  * @param qinfo: query that is verified for.
995ae8c6e27Sflorian  * @param prove_does_not_exist: If true, then if the closest encloser
996ae8c6e27Sflorian  * 	turns out to be qname, then null is returned.
997ae8c6e27Sflorian  * 	If set true, and the return value is true, then you can be
998ae8c6e27Sflorian  * 	certain that the ce.nc_rrset and ce.nc_rr are set properly.
999ae8c6e27Sflorian  * @param ce: closest encloser information is returned in here.
1000fed3efa7Sflorian  * @param calculations: pointer to the current NSEC3 hash calculations.
1001ae8c6e27Sflorian  * @return bogus if no closest encloser could be proven.
1002ae8c6e27Sflorian  * 	secure if a closest encloser could be proven, ce is set.
1003ae8c6e27Sflorian  * 	insecure if the closest-encloser candidate turns out to prove
1004ae8c6e27Sflorian  * 		that an insecure delegation exists above the qname.
1005fed3efa7Sflorian  *	unchecked if no more hash calculations are allowed at this point.
1006ae8c6e27Sflorian  */
1007ae8c6e27Sflorian static enum sec_status
1008ae8c6e27Sflorian nsec3_prove_closest_encloser(struct module_env* env, struct nsec3_filter* flt,
1009fed3efa7Sflorian 	struct nsec3_cache_table* ct, struct query_info* qinfo,
1010fed3efa7Sflorian 	int prove_does_not_exist, struct ce_response* ce, int* calculations)
1011ae8c6e27Sflorian {
1012ae8c6e27Sflorian 	uint8_t* nc;
1013ae8c6e27Sflorian 	size_t nc_len;
1014ae8c6e27Sflorian 	/* robust: clean out ce, in case it gets abused later */
1015ae8c6e27Sflorian 	memset(ce, 0, sizeof(*ce));
1016ae8c6e27Sflorian 
1017fed3efa7Sflorian 	if(!nsec3_find_closest_encloser(env, flt, ct, qinfo, ce, calculations)) {
1018fed3efa7Sflorian 		if(*calculations == MAX_NSEC3_ERRORS) {
1019fed3efa7Sflorian 			verbose(VERB_ALGO, "nsec3 proveClosestEncloser: could "
1020fed3efa7Sflorian 				"not find a candidate for the closest "
1021fed3efa7Sflorian 				"encloser; all attempted hash calculations "
1022fed3efa7Sflorian 				"were erroneous; bogus");
1023fed3efa7Sflorian 			return sec_status_bogus;
1024fed3efa7Sflorian 		} else if(*calculations >= MAX_NSEC3_CALCULATIONS) {
1025fed3efa7Sflorian 			verbose(VERB_ALGO, "nsec3 proveClosestEncloser: could "
1026fed3efa7Sflorian 				"not find a candidate for the closest "
1027fed3efa7Sflorian 				"encloser; reached MAX_NSEC3_CALCULATIONS "
1028fed3efa7Sflorian 				"(%d); unchecked still",
1029fed3efa7Sflorian 				MAX_NSEC3_CALCULATIONS);
1030fed3efa7Sflorian 			return sec_status_unchecked;
1031fed3efa7Sflorian 		}
1032ae8c6e27Sflorian 		verbose(VERB_ALGO, "nsec3 proveClosestEncloser: could "
1033ae8c6e27Sflorian 			"not find a candidate for the closest encloser.");
1034ae8c6e27Sflorian 		return sec_status_bogus;
1035ae8c6e27Sflorian 	}
1036ae8c6e27Sflorian 	log_nametypeclass(VERB_ALGO, "ce candidate", ce->ce, 0, 0);
1037ae8c6e27Sflorian 
1038ae8c6e27Sflorian 	if(query_dname_compare(ce->ce, qinfo->qname) == 0) {
1039ae8c6e27Sflorian 		if(prove_does_not_exist) {
1040ae8c6e27Sflorian 			verbose(VERB_ALGO, "nsec3 proveClosestEncloser: "
1041ae8c6e27Sflorian 				"proved that qname existed, bad");
1042ae8c6e27Sflorian 			return sec_status_bogus;
1043ae8c6e27Sflorian 		}
1044ae8c6e27Sflorian 		/* otherwise, we need to nothing else to prove that qname
1045ae8c6e27Sflorian 		 * is its own closest encloser. */
1046ae8c6e27Sflorian 		return sec_status_secure;
1047ae8c6e27Sflorian 	}
1048ae8c6e27Sflorian 
1049ae8c6e27Sflorian 	/* If the closest encloser is actually a delegation, then the
1050ae8c6e27Sflorian 	 * response should have been a referral. If it is a DNAME, then
1051ae8c6e27Sflorian 	 * it should have been a DNAME response. */
1052ae8c6e27Sflorian 	if(nsec3_has_type(ce->ce_rrset, ce->ce_rr, LDNS_RR_TYPE_NS) &&
1053ae8c6e27Sflorian 		!nsec3_has_type(ce->ce_rrset, ce->ce_rr, LDNS_RR_TYPE_SOA)) {
1054ae8c6e27Sflorian 		if(!nsec3_has_type(ce->ce_rrset, ce->ce_rr, LDNS_RR_TYPE_DS)) {
1055ae8c6e27Sflorian 			verbose(VERB_ALGO, "nsec3 proveClosestEncloser: "
1056ae8c6e27Sflorian 				"closest encloser is insecure delegation");
1057ae8c6e27Sflorian 			return sec_status_insecure;
1058ae8c6e27Sflorian 		}
1059ae8c6e27Sflorian 		verbose(VERB_ALGO, "nsec3 proveClosestEncloser: closest "
1060ae8c6e27Sflorian 			"encloser was a delegation, bad");
1061ae8c6e27Sflorian 		return sec_status_bogus;
1062ae8c6e27Sflorian 	}
1063ae8c6e27Sflorian 	if(nsec3_has_type(ce->ce_rrset, ce->ce_rr, LDNS_RR_TYPE_DNAME)) {
1064ae8c6e27Sflorian 		verbose(VERB_ALGO, "nsec3 proveClosestEncloser: closest "
1065ae8c6e27Sflorian 			"encloser was a DNAME, bad");
1066ae8c6e27Sflorian 		return sec_status_bogus;
1067ae8c6e27Sflorian 	}
1068ae8c6e27Sflorian 
1069ae8c6e27Sflorian 	/* Otherwise, we need to show that the next closer name is covered. */
1070ae8c6e27Sflorian 	next_closer(qinfo->qname, qinfo->qname_len, ce->ce, &nc, &nc_len);
1071ae8c6e27Sflorian 	if(!find_covering_nsec3(env, flt, ct, nc, nc_len,
1072fed3efa7Sflorian 		&ce->nc_rrset, &ce->nc_rr, calculations)) {
1073fed3efa7Sflorian 		if(*calculations == MAX_NSEC3_ERRORS) {
1074fed3efa7Sflorian 			verbose(VERB_ALGO, "nsec3: Could not find proof that the "
1075fed3efa7Sflorian 				"candidate encloser was the closest encloser; "
1076fed3efa7Sflorian 				"all attempted hash calculations were "
1077fed3efa7Sflorian 				"erroneous; bogus");
1078fed3efa7Sflorian 			return sec_status_bogus;
1079fed3efa7Sflorian 		} else if(*calculations >= MAX_NSEC3_CALCULATIONS) {
1080fed3efa7Sflorian 			verbose(VERB_ALGO, "nsec3: Could not find proof that the "
1081fed3efa7Sflorian 				"candidate encloser was the closest encloser; "
1082fed3efa7Sflorian 				"reached MAX_NSEC3_CALCULATIONS (%d); "
1083fed3efa7Sflorian 				"unchecked still",
1084fed3efa7Sflorian 				MAX_NSEC3_CALCULATIONS);
1085fed3efa7Sflorian 			return sec_status_unchecked;
1086fed3efa7Sflorian 		}
1087ae8c6e27Sflorian 		verbose(VERB_ALGO, "nsec3: Could not find proof that the "
1088ae8c6e27Sflorian 			"candidate encloser was the closest encloser");
1089ae8c6e27Sflorian 		return sec_status_bogus;
1090ae8c6e27Sflorian 	}
1091ae8c6e27Sflorian 	return sec_status_secure;
1092ae8c6e27Sflorian }
1093ae8c6e27Sflorian 
1094ae8c6e27Sflorian /** allocate a wildcard for the closest encloser */
1095ae8c6e27Sflorian static uint8_t*
1096ae8c6e27Sflorian nsec3_ce_wildcard(struct regional* region, uint8_t* ce, size_t celen,
1097ae8c6e27Sflorian 	size_t* len)
1098ae8c6e27Sflorian {
1099ae8c6e27Sflorian 	uint8_t* nm;
1100ae8c6e27Sflorian 	if(celen > LDNS_MAX_DOMAINLEN - 2)
1101ae8c6e27Sflorian 		return 0; /* too long */
1102ae8c6e27Sflorian 	nm = (uint8_t*)regional_alloc(region, celen+2);
1103ae8c6e27Sflorian 	if(!nm) {
1104ae8c6e27Sflorian 		log_err("nsec3 wildcard: out of memory");
1105ae8c6e27Sflorian 		return 0; /* alloc failure */
1106ae8c6e27Sflorian 	}
1107ae8c6e27Sflorian 	nm[0] = 1;
1108ae8c6e27Sflorian 	nm[1] = (uint8_t)'*'; /* wildcard label */
1109ae8c6e27Sflorian 	memmove(nm+2, ce, celen);
1110ae8c6e27Sflorian 	*len = celen+2;
1111ae8c6e27Sflorian 	return nm;
1112ae8c6e27Sflorian }
1113ae8c6e27Sflorian 
1114ae8c6e27Sflorian /** Do the name error proof */
1115ae8c6e27Sflorian static enum sec_status
1116ae8c6e27Sflorian nsec3_do_prove_nameerror(struct module_env* env, struct nsec3_filter* flt,
1117fed3efa7Sflorian 	struct nsec3_cache_table* ct, struct query_info* qinfo, int* calc)
1118ae8c6e27Sflorian {
1119ae8c6e27Sflorian 	struct ce_response ce;
1120ae8c6e27Sflorian 	uint8_t* wc;
1121ae8c6e27Sflorian 	size_t wclen;
1122ae8c6e27Sflorian 	struct ub_packed_rrset_key* wc_rrset;
1123ae8c6e27Sflorian 	int wc_rr;
1124ae8c6e27Sflorian 	enum sec_status sec;
1125ae8c6e27Sflorian 
1126ae8c6e27Sflorian 	/* First locate and prove the closest encloser to qname. We will
1127ae8c6e27Sflorian 	 * use the variant that fails if the closest encloser turns out
1128ae8c6e27Sflorian 	 * to be qname. */
1129fed3efa7Sflorian 	sec = nsec3_prove_closest_encloser(env, flt, ct, qinfo, 1, &ce, calc);
1130ae8c6e27Sflorian 	if(sec != sec_status_secure) {
1131ae8c6e27Sflorian 		if(sec == sec_status_bogus)
1132ae8c6e27Sflorian 			verbose(VERB_ALGO, "nsec3 nameerror proof: failed "
1133ae8c6e27Sflorian 				"to prove a closest encloser");
1134fed3efa7Sflorian 		else if(sec == sec_status_unchecked)
1135fed3efa7Sflorian 			verbose(VERB_ALGO, "nsec3 nameerror proof: will "
1136fed3efa7Sflorian 				"continue proving closest encloser after "
1137fed3efa7Sflorian 				"suspend");
1138ae8c6e27Sflorian 		else 	verbose(VERB_ALGO, "nsec3 nameerror proof: closest "
1139ae8c6e27Sflorian 				"nsec3 is an insecure delegation");
1140ae8c6e27Sflorian 		return sec;
1141ae8c6e27Sflorian 	}
1142ae8c6e27Sflorian 	log_nametypeclass(VERB_ALGO, "nsec3 nameerror: proven ce=", ce.ce,0,0);
1143ae8c6e27Sflorian 
1144ae8c6e27Sflorian 	/* At this point, we know that qname does not exist. Now we need
1145ae8c6e27Sflorian 	 * to prove that the wildcard does not exist. */
1146ae8c6e27Sflorian 	log_assert(ce.ce);
1147fed3efa7Sflorian 	wc = nsec3_ce_wildcard(ct->region, ce.ce, ce.ce_len, &wclen);
1148fed3efa7Sflorian 	if(!wc) {
1149fed3efa7Sflorian 		verbose(VERB_ALGO, "nsec3 nameerror proof: could not prove "
1150fed3efa7Sflorian 			"that the applicable wildcard did not exist.");
1151fed3efa7Sflorian 		return sec_status_bogus;
1152fed3efa7Sflorian 	}
1153fed3efa7Sflorian 	if(!find_covering_nsec3(env, flt, ct, wc, wclen, &wc_rrset, &wc_rr, calc)) {
1154fed3efa7Sflorian 		if(*calc == MAX_NSEC3_ERRORS) {
1155fed3efa7Sflorian 			verbose(VERB_ALGO, "nsec3 nameerror proof: could not prove "
1156fed3efa7Sflorian 				"that the applicable wildcard did not exist; "
1157fed3efa7Sflorian 				"all attempted hash calculations were "
1158fed3efa7Sflorian 				"erroneous; bogus");
1159fed3efa7Sflorian 			return sec_status_bogus;
1160fed3efa7Sflorian 		} else if(*calc >= MAX_NSEC3_CALCULATIONS) {
1161fed3efa7Sflorian 			verbose(VERB_ALGO, "nsec3 nameerror proof: could not prove "
1162fed3efa7Sflorian 				"that the applicable wildcard did not exist; "
1163fed3efa7Sflorian 				"reached MAX_NSEC3_CALCULATIONS (%d); "
1164fed3efa7Sflorian 				"unchecked still",
1165fed3efa7Sflorian 				MAX_NSEC3_CALCULATIONS);
1166fed3efa7Sflorian 			return sec_status_unchecked;
1167fed3efa7Sflorian 		}
1168ae8c6e27Sflorian 		verbose(VERB_ALGO, "nsec3 nameerror proof: could not prove "
1169ae8c6e27Sflorian 			"that the applicable wildcard did not exist.");
1170ae8c6e27Sflorian 		return sec_status_bogus;
1171ae8c6e27Sflorian 	}
1172ae8c6e27Sflorian 
1173ae8c6e27Sflorian 	if(ce.nc_rrset && nsec3_has_optout(ce.nc_rrset, ce.nc_rr)) {
1174ae8c6e27Sflorian 		verbose(VERB_ALGO, "nsec3 nameerror proof: nc has optout");
1175ae8c6e27Sflorian 		return sec_status_insecure;
1176ae8c6e27Sflorian 	}
1177ae8c6e27Sflorian 	return sec_status_secure;
1178ae8c6e27Sflorian }
1179ae8c6e27Sflorian 
1180ae8c6e27Sflorian enum sec_status
1181ae8c6e27Sflorian nsec3_prove_nameerror(struct module_env* env, struct val_env* ve,
1182ae8c6e27Sflorian 	struct ub_packed_rrset_key** list, size_t num,
1183fed3efa7Sflorian 	struct query_info* qinfo, struct key_entry_key* kkey,
1184fed3efa7Sflorian 	struct nsec3_cache_table* ct, int* calc)
1185ae8c6e27Sflorian {
1186ae8c6e27Sflorian 	struct nsec3_filter flt;
1187ae8c6e27Sflorian 
1188ae8c6e27Sflorian 	if(!list || num == 0 || !kkey || !key_entry_isgood(kkey))
1189ae8c6e27Sflorian 		return sec_status_bogus; /* no valid NSEC3s, bogus */
1190ae8c6e27Sflorian 	filter_init(&flt, list, num, qinfo); /* init RR iterator */
1191ae8c6e27Sflorian 	if(!flt.zone)
1192ae8c6e27Sflorian 		return sec_status_bogus; /* no RRs */
1193ae8c6e27Sflorian 	if(nsec3_iteration_count_high(ve, &flt, kkey))
1194ae8c6e27Sflorian 		return sec_status_insecure; /* iteration count too high */
1195ae8c6e27Sflorian 	log_nametypeclass(VERB_ALGO, "start nsec3 nameerror proof, zone",
1196ae8c6e27Sflorian 		flt.zone, 0, 0);
1197fed3efa7Sflorian 	return nsec3_do_prove_nameerror(env, &flt, ct, qinfo, calc);
1198ae8c6e27Sflorian }
1199ae8c6e27Sflorian 
1200ae8c6e27Sflorian /*
1201ae8c6e27Sflorian  * No code to handle qtype=NSEC3 specially.
1202ae8c6e27Sflorian  * This existed in early drafts, but was later (-05) removed.
1203ae8c6e27Sflorian  */
1204ae8c6e27Sflorian 
1205ae8c6e27Sflorian /** Do the nodata proof */
1206ae8c6e27Sflorian static enum sec_status
1207ae8c6e27Sflorian nsec3_do_prove_nodata(struct module_env* env, struct nsec3_filter* flt,
1208fed3efa7Sflorian 	struct nsec3_cache_table* ct, struct query_info* qinfo,
1209fed3efa7Sflorian 	int* calc)
1210ae8c6e27Sflorian {
1211ae8c6e27Sflorian 	struct ce_response ce;
1212ae8c6e27Sflorian 	uint8_t* wc;
1213ae8c6e27Sflorian 	size_t wclen;
1214ae8c6e27Sflorian 	struct ub_packed_rrset_key* rrset;
1215ae8c6e27Sflorian 	int rr;
1216ae8c6e27Sflorian 	enum sec_status sec;
1217ae8c6e27Sflorian 
1218ae8c6e27Sflorian 	if(find_matching_nsec3(env, flt, ct, qinfo->qname, qinfo->qname_len,
1219fed3efa7Sflorian 		&rrset, &rr, calc)) {
1220ae8c6e27Sflorian 		/* cases 1 and 2 */
1221ae8c6e27Sflorian 		if(nsec3_has_type(rrset, rr, qinfo->qtype)) {
1222ae8c6e27Sflorian 			verbose(VERB_ALGO, "proveNodata: Matching NSEC3 "
1223ae8c6e27Sflorian 				"proved that type existed, bogus");
1224ae8c6e27Sflorian 			return sec_status_bogus;
1225ae8c6e27Sflorian 		} else if(nsec3_has_type(rrset, rr, LDNS_RR_TYPE_CNAME)) {
1226ae8c6e27Sflorian 			verbose(VERB_ALGO, "proveNodata: Matching NSEC3 "
1227ae8c6e27Sflorian 				"proved that a CNAME existed, bogus");
1228ae8c6e27Sflorian 			return sec_status_bogus;
1229ae8c6e27Sflorian 		}
1230ae8c6e27Sflorian 
1231ae8c6e27Sflorian 		/*
1232ae8c6e27Sflorian 		 * If type DS: filter_init zone find already found a parent
1233ae8c6e27Sflorian 		 *   zone, so this nsec3 is from a parent zone.
1234ae8c6e27Sflorian 		 *   o can be not a delegation (unusual query for normal name,
1235ae8c6e27Sflorian 		 *   	no DS anyway, but we can verify that).
1236ae8c6e27Sflorian 		 *   o can be a delegation (which is the usual DS check).
1237ae8c6e27Sflorian 		 *   o may not have the SOA bit set (only the top of the
1238ae8c6e27Sflorian 		 *   	zone, which must have been above the name, has that).
1239ae8c6e27Sflorian 		 *   	Except for the root; which is checked by itself.
1240ae8c6e27Sflorian 		 *
1241ae8c6e27Sflorian 		 * If not type DS: matching nsec3 must not be a delegation.
1242ae8c6e27Sflorian 		 */
1243ae8c6e27Sflorian 		if(qinfo->qtype == LDNS_RR_TYPE_DS && qinfo->qname_len != 1
1244ae8c6e27Sflorian 			&& nsec3_has_type(rrset, rr, LDNS_RR_TYPE_SOA) &&
1245ae8c6e27Sflorian 			!dname_is_root(qinfo->qname)) {
1246ae8c6e27Sflorian 			verbose(VERB_ALGO, "proveNodata: apex NSEC3 "
1247ae8c6e27Sflorian 				"abused for no DS proof, bogus");
1248ae8c6e27Sflorian 			return sec_status_bogus;
1249ae8c6e27Sflorian 		} else if(qinfo->qtype != LDNS_RR_TYPE_DS &&
1250ae8c6e27Sflorian 			nsec3_has_type(rrset, rr, LDNS_RR_TYPE_NS) &&
1251ae8c6e27Sflorian 			!nsec3_has_type(rrset, rr, LDNS_RR_TYPE_SOA)) {
1252ae8c6e27Sflorian 			if(!nsec3_has_type(rrset, rr, LDNS_RR_TYPE_DS)) {
1253ae8c6e27Sflorian 				verbose(VERB_ALGO, "proveNodata: matching "
1254ae8c6e27Sflorian 					"NSEC3 is insecure delegation");
1255ae8c6e27Sflorian 				return sec_status_insecure;
1256ae8c6e27Sflorian 			}
1257ae8c6e27Sflorian 			verbose(VERB_ALGO, "proveNodata: matching "
1258ae8c6e27Sflorian 				"NSEC3 is a delegation, bogus");
1259ae8c6e27Sflorian 			return sec_status_bogus;
1260ae8c6e27Sflorian 		}
1261ae8c6e27Sflorian 		return sec_status_secure;
1262ae8c6e27Sflorian 	}
1263fed3efa7Sflorian 	if(*calc == MAX_NSEC3_ERRORS) {
1264fed3efa7Sflorian 		verbose(VERB_ALGO, "proveNodata: all attempted hash "
1265fed3efa7Sflorian 			"calculations were erroneous while finding a matching "
1266fed3efa7Sflorian 			"NSEC3, bogus");
1267fed3efa7Sflorian 		return sec_status_bogus;
1268fed3efa7Sflorian 	} else if(*calc >= MAX_NSEC3_CALCULATIONS) {
1269fed3efa7Sflorian 		verbose(VERB_ALGO, "proveNodata: reached "
1270fed3efa7Sflorian 			"MAX_NSEC3_CALCULATIONS (%d) while finding a "
1271fed3efa7Sflorian 			"matching NSEC3; unchecked still",
1272fed3efa7Sflorian 			MAX_NSEC3_CALCULATIONS);
1273fed3efa7Sflorian 		return sec_status_unchecked;
1274fed3efa7Sflorian 	}
1275ae8c6e27Sflorian 
1276ae8c6e27Sflorian 	/* For cases 3 - 5, we need the proven closest encloser, and it
1277ae8c6e27Sflorian 	 * can't match qname. Although, at this point, we know that it
1278ae8c6e27Sflorian 	 * won't since we just checked that. */
1279fed3efa7Sflorian 	sec = nsec3_prove_closest_encloser(env, flt, ct, qinfo, 1, &ce, calc);
1280ae8c6e27Sflorian 	if(sec == sec_status_bogus) {
1281ae8c6e27Sflorian 		verbose(VERB_ALGO, "proveNodata: did not match qname, "
1282ae8c6e27Sflorian 		          "nor found a proven closest encloser.");
1283ae8c6e27Sflorian 		return sec_status_bogus;
1284ae8c6e27Sflorian 	} else if(sec==sec_status_insecure && qinfo->qtype!=LDNS_RR_TYPE_DS){
1285ae8c6e27Sflorian 		verbose(VERB_ALGO, "proveNodata: closest nsec3 is insecure "
1286ae8c6e27Sflorian 		          "delegation.");
1287ae8c6e27Sflorian 		return sec_status_insecure;
1288fed3efa7Sflorian 	} else if(sec==sec_status_unchecked) {
1289fed3efa7Sflorian 		return sec_status_unchecked;
1290ae8c6e27Sflorian 	}
1291ae8c6e27Sflorian 
1292ae8c6e27Sflorian 	/* Case 3: removed */
1293ae8c6e27Sflorian 
1294ae8c6e27Sflorian 	/* Case 4: */
1295ae8c6e27Sflorian 	log_assert(ce.ce);
1296fed3efa7Sflorian 	wc = nsec3_ce_wildcard(ct->region, ce.ce, ce.ce_len, &wclen);
1297fed3efa7Sflorian 	if(wc && find_matching_nsec3(env, flt, ct, wc, wclen, &rrset, &rr,
1298fed3efa7Sflorian 		calc)) {
1299ae8c6e27Sflorian 		/* found wildcard */
1300ae8c6e27Sflorian 		if(nsec3_has_type(rrset, rr, qinfo->qtype)) {
1301ae8c6e27Sflorian 			verbose(VERB_ALGO, "nsec3 nodata proof: matching "
1302ae8c6e27Sflorian 				"wildcard had qtype, bogus");
1303ae8c6e27Sflorian 			return sec_status_bogus;
1304ae8c6e27Sflorian 		} else if(nsec3_has_type(rrset, rr, LDNS_RR_TYPE_CNAME)) {
1305ae8c6e27Sflorian 			verbose(VERB_ALGO, "nsec3 nodata proof: matching "
1306ae8c6e27Sflorian 				"wildcard had a CNAME, bogus");
1307ae8c6e27Sflorian 			return sec_status_bogus;
1308ae8c6e27Sflorian 		}
1309ae8c6e27Sflorian 		if(qinfo->qtype == LDNS_RR_TYPE_DS && qinfo->qname_len != 1
1310ae8c6e27Sflorian 			&& nsec3_has_type(rrset, rr, LDNS_RR_TYPE_SOA)) {
1311ae8c6e27Sflorian 			verbose(VERB_ALGO, "nsec3 nodata proof: matching "
1312ae8c6e27Sflorian 				"wildcard for no DS proof has a SOA, bogus");
1313ae8c6e27Sflorian 			return sec_status_bogus;
1314ae8c6e27Sflorian 		} else if(qinfo->qtype != LDNS_RR_TYPE_DS &&
1315ae8c6e27Sflorian 			nsec3_has_type(rrset, rr, LDNS_RR_TYPE_NS) &&
1316ae8c6e27Sflorian 			!nsec3_has_type(rrset, rr, LDNS_RR_TYPE_SOA)) {
1317ae8c6e27Sflorian 			verbose(VERB_ALGO, "nsec3 nodata proof: matching "
1318ae8c6e27Sflorian 				"wildcard is a delegation, bogus");
1319ae8c6e27Sflorian 			return sec_status_bogus;
1320ae8c6e27Sflorian 		}
1321ae8c6e27Sflorian 		/* everything is peachy keen, except for optout spans */
1322ae8c6e27Sflorian 		if(ce.nc_rrset && nsec3_has_optout(ce.nc_rrset, ce.nc_rr)) {
1323ae8c6e27Sflorian 			verbose(VERB_ALGO, "nsec3 nodata proof: matching "
1324ae8c6e27Sflorian 				"wildcard is in optout range, insecure");
1325ae8c6e27Sflorian 			return sec_status_insecure;
1326ae8c6e27Sflorian 		}
1327ae8c6e27Sflorian 		return sec_status_secure;
1328ae8c6e27Sflorian 	}
1329fed3efa7Sflorian 	if(*calc == MAX_NSEC3_ERRORS) {
1330fed3efa7Sflorian 		verbose(VERB_ALGO, "nsec3 nodata proof: all attempted hash "
1331fed3efa7Sflorian 			"calculations were erroneous while matching "
1332fed3efa7Sflorian 			"wildcard, bogus");
1333fed3efa7Sflorian 		return sec_status_bogus;
1334fed3efa7Sflorian 	} else if(*calc >= MAX_NSEC3_CALCULATIONS) {
1335fed3efa7Sflorian 		verbose(VERB_ALGO, "nsec3 nodata proof: reached "
1336fed3efa7Sflorian 			"MAX_NSEC3_CALCULATIONS (%d) while matching "
1337fed3efa7Sflorian 			"wildcard, unchecked still",
1338fed3efa7Sflorian 			MAX_NSEC3_CALCULATIONS);
1339fed3efa7Sflorian 		return sec_status_unchecked;
1340fed3efa7Sflorian 	}
1341ae8c6e27Sflorian 
1342ae8c6e27Sflorian 	/* Case 5: */
1343ae8c6e27Sflorian 	/* Due to forwarders, cnames, and other collating effects, we
1344ae8c6e27Sflorian 	 * can see the ordinary unsigned data from a zone beneath an
1345ae8c6e27Sflorian 	 * insecure delegation under an optout here */
1346ae8c6e27Sflorian 	if(!ce.nc_rrset) {
1347ae8c6e27Sflorian 		verbose(VERB_ALGO, "nsec3 nodata proof: no next closer nsec3");
1348ae8c6e27Sflorian 		return sec_status_bogus;
1349ae8c6e27Sflorian 	}
1350ae8c6e27Sflorian 
1351ae8c6e27Sflorian 	/* We need to make sure that the covering NSEC3 is opt-out. */
1352ae8c6e27Sflorian 	log_assert(ce.nc_rrset);
1353ae8c6e27Sflorian 	if(!nsec3_has_optout(ce.nc_rrset, ce.nc_rr)) {
1354ae8c6e27Sflorian 		if(qinfo->qtype == LDNS_RR_TYPE_DS)
1355ae8c6e27Sflorian 		  verbose(VERB_ALGO, "proveNodata: covering NSEC3 was not "
1356ae8c6e27Sflorian 			"opt-out in an opt-out DS NOERROR/NODATA case.");
1357ae8c6e27Sflorian 		else verbose(VERB_ALGO, "proveNodata: could not find matching "
1358ae8c6e27Sflorian 			"NSEC3, nor matching wildcard, nor optout NSEC3 "
1359ae8c6e27Sflorian 			"-- no more options, bogus.");
1360ae8c6e27Sflorian 		return sec_status_bogus;
1361ae8c6e27Sflorian 	}
1362ae8c6e27Sflorian 	/* RFC5155 section 9.2: if nc has optout then no AD flag set */
1363ae8c6e27Sflorian 	return sec_status_insecure;
1364ae8c6e27Sflorian }
1365ae8c6e27Sflorian 
1366ae8c6e27Sflorian enum sec_status
1367ae8c6e27Sflorian nsec3_prove_nodata(struct module_env* env, struct val_env* ve,
1368ae8c6e27Sflorian 	struct ub_packed_rrset_key** list, size_t num,
1369fed3efa7Sflorian 	struct query_info* qinfo, struct key_entry_key* kkey,
1370fed3efa7Sflorian 	struct nsec3_cache_table* ct, int* calc)
1371ae8c6e27Sflorian {
1372ae8c6e27Sflorian 	struct nsec3_filter flt;
1373ae8c6e27Sflorian 
1374ae8c6e27Sflorian 	if(!list || num == 0 || !kkey || !key_entry_isgood(kkey))
1375ae8c6e27Sflorian 		return sec_status_bogus; /* no valid NSEC3s, bogus */
1376ae8c6e27Sflorian 	filter_init(&flt, list, num, qinfo); /* init RR iterator */
1377ae8c6e27Sflorian 	if(!flt.zone)
1378ae8c6e27Sflorian 		return sec_status_bogus; /* no RRs */
1379ae8c6e27Sflorian 	if(nsec3_iteration_count_high(ve, &flt, kkey))
1380ae8c6e27Sflorian 		return sec_status_insecure; /* iteration count too high */
1381fed3efa7Sflorian 	return nsec3_do_prove_nodata(env, &flt, ct, qinfo, calc);
1382ae8c6e27Sflorian }
1383ae8c6e27Sflorian 
1384ae8c6e27Sflorian enum sec_status
1385ae8c6e27Sflorian nsec3_prove_wildcard(struct module_env* env, struct val_env* ve,
1386ae8c6e27Sflorian         struct ub_packed_rrset_key** list, size_t num,
1387fed3efa7Sflorian 	struct query_info* qinfo, struct key_entry_key* kkey, uint8_t* wc,
1388fed3efa7Sflorian 	struct nsec3_cache_table* ct, int* calc)
1389ae8c6e27Sflorian {
1390ae8c6e27Sflorian 	struct nsec3_filter flt;
1391ae8c6e27Sflorian 	struct ce_response ce;
1392ae8c6e27Sflorian 	uint8_t* nc;
1393ae8c6e27Sflorian 	size_t nc_len;
1394ae8c6e27Sflorian 	size_t wclen;
1395ae8c6e27Sflorian 	(void)dname_count_size_labels(wc, &wclen);
1396ae8c6e27Sflorian 
1397ae8c6e27Sflorian 	if(!list || num == 0 || !kkey || !key_entry_isgood(kkey))
1398ae8c6e27Sflorian 		return sec_status_bogus; /* no valid NSEC3s, bogus */
1399ae8c6e27Sflorian 	filter_init(&flt, list, num, qinfo); /* init RR iterator */
1400ae8c6e27Sflorian 	if(!flt.zone)
1401ae8c6e27Sflorian 		return sec_status_bogus; /* no RRs */
1402ae8c6e27Sflorian 	if(nsec3_iteration_count_high(ve, &flt, kkey))
1403ae8c6e27Sflorian 		return sec_status_insecure; /* iteration count too high */
1404ae8c6e27Sflorian 
1405ae8c6e27Sflorian 	/* We know what the (purported) closest encloser is by just
1406ae8c6e27Sflorian 	 * looking at the supposed generating wildcard.
1407ae8c6e27Sflorian 	 * The *. has already been removed from the wc name.
1408ae8c6e27Sflorian 	 */
1409ae8c6e27Sflorian 	memset(&ce, 0, sizeof(ce));
1410ae8c6e27Sflorian 	ce.ce = wc;
1411ae8c6e27Sflorian 	ce.ce_len = wclen;
1412ae8c6e27Sflorian 
1413ae8c6e27Sflorian 	/* Now we still need to prove that the original data did not exist.
1414ae8c6e27Sflorian 	 * Otherwise, we need to show that the next closer name is covered. */
1415ae8c6e27Sflorian 	next_closer(qinfo->qname, qinfo->qname_len, ce.ce, &nc, &nc_len);
1416fed3efa7Sflorian 	if(!find_covering_nsec3(env, &flt, ct, nc, nc_len,
1417fed3efa7Sflorian 		&ce.nc_rrset, &ce.nc_rr, calc)) {
1418fed3efa7Sflorian 		if(*calc == MAX_NSEC3_ERRORS) {
1419fed3efa7Sflorian 			verbose(VERB_ALGO, "proveWildcard: did not find a "
1420fed3efa7Sflorian 				"covering NSEC3 that covered the next closer "
1421fed3efa7Sflorian 				"name; all attempted hash calculations were "
1422fed3efa7Sflorian 				"erroneous; bogus");
1423fed3efa7Sflorian 			return sec_status_bogus;
1424fed3efa7Sflorian 		} else if(*calc >= MAX_NSEC3_CALCULATIONS) {
1425fed3efa7Sflorian 			verbose(VERB_ALGO, "proveWildcard: did not find a "
1426fed3efa7Sflorian 				"covering NSEC3 that covered the next closer "
1427fed3efa7Sflorian 				"name; reached MAX_NSEC3_CALCULATIONS "
1428fed3efa7Sflorian 				"(%d); unchecked still",
1429fed3efa7Sflorian 				MAX_NSEC3_CALCULATIONS);
1430fed3efa7Sflorian 			return sec_status_unchecked;
1431fed3efa7Sflorian 		}
1432ae8c6e27Sflorian 		verbose(VERB_ALGO, "proveWildcard: did not find a covering "
1433ae8c6e27Sflorian 			"NSEC3 that covered the next closer name.");
1434ae8c6e27Sflorian 		return sec_status_bogus;
1435ae8c6e27Sflorian 	}
1436ae8c6e27Sflorian 	if(ce.nc_rrset && nsec3_has_optout(ce.nc_rrset, ce.nc_rr)) {
1437ae8c6e27Sflorian 		verbose(VERB_ALGO, "proveWildcard: NSEC3 optout");
1438ae8c6e27Sflorian 		return sec_status_insecure;
1439ae8c6e27Sflorian 	}
1440ae8c6e27Sflorian 	return sec_status_secure;
1441ae8c6e27Sflorian }
1442ae8c6e27Sflorian 
1443ae8c6e27Sflorian /** test if list is all secure */
1444ae8c6e27Sflorian static int
1445ae8c6e27Sflorian list_is_secure(struct module_env* env, struct val_env* ve,
1446ae8c6e27Sflorian 	struct ub_packed_rrset_key** list, size_t num,
14477a05b9dfSflorian 	struct key_entry_key* kkey, char** reason, sldns_ede_code *reason_bogus,
1448*7037e34cSflorian 	struct module_qstate* qstate, char* reasonbuf, size_t reasonlen)
1449ae8c6e27Sflorian {
1450ae8c6e27Sflorian 	struct packed_rrset_data* d;
1451ae8c6e27Sflorian 	size_t i;
1452fed3efa7Sflorian 	int verified = 0;
1453ae8c6e27Sflorian 	for(i=0; i<num; i++) {
1454ae8c6e27Sflorian 		d = (struct packed_rrset_data*)list[i]->entry.data;
1455ae8c6e27Sflorian 		if(list[i]->rk.type != htons(LDNS_RR_TYPE_NSEC3))
1456ae8c6e27Sflorian 			continue;
1457ae8c6e27Sflorian 		if(d->security == sec_status_secure)
1458ae8c6e27Sflorian 			continue;
1459ae8c6e27Sflorian 		rrset_check_sec_status(env->rrset_cache, list[i], *env->now);
1460ae8c6e27Sflorian 		if(d->security == sec_status_secure)
1461ae8c6e27Sflorian 			continue;
1462ae8c6e27Sflorian 		d->security = val_verify_rrset_entry(env, ve, list[i], kkey,
1463fed3efa7Sflorian 			reason, reason_bogus, LDNS_SECTION_AUTHORITY, qstate,
1464*7037e34cSflorian 			&verified, reasonbuf, reasonlen);
1465ae8c6e27Sflorian 		if(d->security != sec_status_secure) {
1466ae8c6e27Sflorian 			verbose(VERB_ALGO, "NSEC3 did not verify");
1467ae8c6e27Sflorian 			return 0;
1468ae8c6e27Sflorian 		}
1469ae8c6e27Sflorian 		rrset_update_sec_status(env->rrset_cache, list[i], *env->now);
1470ae8c6e27Sflorian 	}
1471ae8c6e27Sflorian 	return 1;
1472ae8c6e27Sflorian }
1473ae8c6e27Sflorian 
1474ae8c6e27Sflorian enum sec_status
1475ae8c6e27Sflorian nsec3_prove_nods(struct module_env* env, struct val_env* ve,
1476ae8c6e27Sflorian 	struct ub_packed_rrset_key** list, size_t num,
1477ae8c6e27Sflorian 	struct query_info* qinfo, struct key_entry_key* kkey, char** reason,
1478fed3efa7Sflorian 	sldns_ede_code* reason_bogus, struct module_qstate* qstate,
1479*7037e34cSflorian 	struct nsec3_cache_table* ct, char* reasonbuf, size_t reasonlen)
1480ae8c6e27Sflorian {
1481ae8c6e27Sflorian 	struct nsec3_filter flt;
1482ae8c6e27Sflorian 	struct ce_response ce;
1483ae8c6e27Sflorian 	struct ub_packed_rrset_key* rrset;
1484ae8c6e27Sflorian 	int rr;
1485fed3efa7Sflorian 	int calc = 0;
1486fed3efa7Sflorian 	enum sec_status sec;
1487fed3efa7Sflorian 
1488ae8c6e27Sflorian 	log_assert(qinfo->qtype == LDNS_RR_TYPE_DS);
1489ae8c6e27Sflorian 
1490ae8c6e27Sflorian 	if(!list || num == 0 || !kkey || !key_entry_isgood(kkey)) {
1491ae8c6e27Sflorian 		*reason = "no valid NSEC3s";
1492ae8c6e27Sflorian 		return sec_status_bogus; /* no valid NSEC3s, bogus */
1493ae8c6e27Sflorian 	}
1494*7037e34cSflorian 	if(!list_is_secure(env, ve, list, num, kkey, reason, reason_bogus,
1495*7037e34cSflorian 		qstate, reasonbuf, reasonlen)) {
14967a05b9dfSflorian 		*reason = "not all NSEC3 records secure";
1497ae8c6e27Sflorian 		return sec_status_bogus; /* not all NSEC3 records secure */
14987a05b9dfSflorian 	}
1499ae8c6e27Sflorian 	filter_init(&flt, list, num, qinfo); /* init RR iterator */
1500ae8c6e27Sflorian 	if(!flt.zone) {
1501ae8c6e27Sflorian 		*reason = "no NSEC3 records";
1502ae8c6e27Sflorian 		return sec_status_bogus; /* no RRs */
1503ae8c6e27Sflorian 	}
1504ae8c6e27Sflorian 	if(nsec3_iteration_count_high(ve, &flt, kkey))
1505ae8c6e27Sflorian 		return sec_status_insecure; /* iteration count too high */
1506ae8c6e27Sflorian 
1507ae8c6e27Sflorian 	/* Look for a matching NSEC3 to qname -- this is the normal
1508ae8c6e27Sflorian 	 * NODATA case. */
1509fed3efa7Sflorian 	if(find_matching_nsec3(env, &flt, ct, qinfo->qname, qinfo->qname_len,
1510fed3efa7Sflorian 		&rrset, &rr, &calc)) {
1511ae8c6e27Sflorian 		/* If the matching NSEC3 has the SOA bit set, it is from
1512ae8c6e27Sflorian 		 * the wrong zone (the child instead of the parent). If
1513ae8c6e27Sflorian 		 * it has the DS bit set, then we were lied to. */
1514ae8c6e27Sflorian 		if(nsec3_has_type(rrset, rr, LDNS_RR_TYPE_SOA) &&
1515ae8c6e27Sflorian 			qinfo->qname_len != 1) {
1516ae8c6e27Sflorian 			verbose(VERB_ALGO, "nsec3 provenods: NSEC3 is from"
1517ae8c6e27Sflorian 				" child zone, bogus");
1518ae8c6e27Sflorian 			*reason = "NSEC3 from child zone";
1519ae8c6e27Sflorian 			return sec_status_bogus;
1520ae8c6e27Sflorian 		} else if(nsec3_has_type(rrset, rr, LDNS_RR_TYPE_DS)) {
1521ae8c6e27Sflorian 			verbose(VERB_ALGO, "nsec3 provenods: NSEC3 has qtype"
1522ae8c6e27Sflorian 				" DS, bogus");
1523ae8c6e27Sflorian 			*reason = "NSEC3 has DS in bitmap";
1524ae8c6e27Sflorian 			return sec_status_bogus;
1525ae8c6e27Sflorian 		}
1526ae8c6e27Sflorian 		/* If the NSEC3 RR doesn't have the NS bit set, then
1527ae8c6e27Sflorian 		 * this wasn't a delegation point. */
1528ae8c6e27Sflorian 		if(!nsec3_has_type(rrset, rr, LDNS_RR_TYPE_NS))
1529ae8c6e27Sflorian 			return sec_status_indeterminate;
1530ae8c6e27Sflorian 		/* Otherwise, this proves no DS. */
1531ae8c6e27Sflorian 		return sec_status_secure;
1532ae8c6e27Sflorian 	}
1533fed3efa7Sflorian 	if(calc == MAX_NSEC3_ERRORS) {
1534fed3efa7Sflorian 		verbose(VERB_ALGO, "nsec3 provenods: all attempted hash "
1535fed3efa7Sflorian 			"calculations were erroneous while finding a matching "
1536fed3efa7Sflorian 			"NSEC3, bogus");
1537fed3efa7Sflorian 		return sec_status_bogus;
1538fed3efa7Sflorian 	} else if(calc >= MAX_NSEC3_CALCULATIONS) {
1539fed3efa7Sflorian 		verbose(VERB_ALGO, "nsec3 provenods: reached "
1540fed3efa7Sflorian 			"MAX_NSEC3_CALCULATIONS (%d) while finding a "
1541fed3efa7Sflorian 			"matching NSEC3, unchecked still",
1542fed3efa7Sflorian 			MAX_NSEC3_CALCULATIONS);
1543fed3efa7Sflorian 		return sec_status_unchecked;
1544fed3efa7Sflorian 	}
1545ae8c6e27Sflorian 
1546ae8c6e27Sflorian 	/* Otherwise, we are probably in the opt-out case. */
1547fed3efa7Sflorian 	sec = nsec3_prove_closest_encloser(env, &flt, ct, qinfo, 1, &ce, &calc);
1548fed3efa7Sflorian 	if(sec == sec_status_unchecked) {
1549fed3efa7Sflorian 		return sec_status_unchecked;
1550fed3efa7Sflorian 	} else if(sec != sec_status_secure) {
1551ae8c6e27Sflorian 		/* an insecure delegation *above* the qname does not prove
1552ae8c6e27Sflorian 		 * anything about this qname exactly, and bogus is bogus */
1553ae8c6e27Sflorian 		verbose(VERB_ALGO, "nsec3 provenods: did not match qname, "
1554ae8c6e27Sflorian 		          "nor found a proven closest encloser.");
1555ae8c6e27Sflorian 		*reason = "no NSEC3 closest encloser";
1556ae8c6e27Sflorian 		return sec_status_bogus;
1557ae8c6e27Sflorian 	}
1558ae8c6e27Sflorian 
1559ae8c6e27Sflorian 	/* robust extra check */
1560ae8c6e27Sflorian 	if(!ce.nc_rrset) {
1561ae8c6e27Sflorian 		verbose(VERB_ALGO, "nsec3 nods proof: no next closer nsec3");
1562ae8c6e27Sflorian 		*reason = "no NSEC3 next closer";
1563ae8c6e27Sflorian 		return sec_status_bogus;
1564ae8c6e27Sflorian 	}
1565ae8c6e27Sflorian 
1566ae8c6e27Sflorian 	/* we had the closest encloser proof, then we need to check that the
1567ae8c6e27Sflorian 	 * covering NSEC3 was opt-out -- the proveClosestEncloser step already
1568ae8c6e27Sflorian 	 * checked to see if the closest encloser was a delegation or DNAME.
1569ae8c6e27Sflorian 	 */
1570ae8c6e27Sflorian 	log_assert(ce.nc_rrset);
1571ae8c6e27Sflorian 	if(!nsec3_has_optout(ce.nc_rrset, ce.nc_rr)) {
1572ae8c6e27Sflorian 		verbose(VERB_ALGO, "nsec3 provenods: covering NSEC3 was not "
1573ae8c6e27Sflorian 			"opt-out in an opt-out DS NOERROR/NODATA case.");
1574ae8c6e27Sflorian 		*reason = "covering NSEC3 was not opt-out in an opt-out "
1575ae8c6e27Sflorian 			"DS NOERROR/NODATA case";
1576ae8c6e27Sflorian 		return sec_status_bogus;
1577ae8c6e27Sflorian 	}
1578ae8c6e27Sflorian 	/* RFC5155 section 9.2: if nc has optout then no AD flag set */
1579ae8c6e27Sflorian 	return sec_status_insecure;
1580ae8c6e27Sflorian }
1581ae8c6e27Sflorian 
1582ae8c6e27Sflorian enum sec_status
1583ae8c6e27Sflorian nsec3_prove_nxornodata(struct module_env* env, struct val_env* ve,
1584ae8c6e27Sflorian 	struct ub_packed_rrset_key** list, size_t num,
1585fed3efa7Sflorian 	struct query_info* qinfo, struct key_entry_key* kkey, int* nodata,
1586fed3efa7Sflorian 	struct  nsec3_cache_table* ct, int* calc)
1587ae8c6e27Sflorian {
1588ae8c6e27Sflorian 	enum sec_status sec, secnx;
1589ae8c6e27Sflorian 	struct nsec3_filter flt;
1590ae8c6e27Sflorian 	*nodata = 0;
1591ae8c6e27Sflorian 
1592ae8c6e27Sflorian 	if(!list || num == 0 || !kkey || !key_entry_isgood(kkey))
1593ae8c6e27Sflorian 		return sec_status_bogus; /* no valid NSEC3s, bogus */
1594ae8c6e27Sflorian 	filter_init(&flt, list, num, qinfo); /* init RR iterator */
1595ae8c6e27Sflorian 	if(!flt.zone)
1596ae8c6e27Sflorian 		return sec_status_bogus; /* no RRs */
1597ae8c6e27Sflorian 	if(nsec3_iteration_count_high(ve, &flt, kkey))
1598ae8c6e27Sflorian 		return sec_status_insecure; /* iteration count too high */
1599ae8c6e27Sflorian 
1600ae8c6e27Sflorian 	/* try nxdomain and nodata after another, while keeping the
1601ae8c6e27Sflorian 	 * hash cache intact */
1602ae8c6e27Sflorian 
1603fed3efa7Sflorian 	secnx = nsec3_do_prove_nameerror(env, &flt, ct, qinfo, calc);
1604ae8c6e27Sflorian 	if(secnx==sec_status_secure)
1605ae8c6e27Sflorian 		return sec_status_secure;
1606fed3efa7Sflorian 	else if(secnx == sec_status_unchecked)
1607fed3efa7Sflorian 		return sec_status_unchecked;
1608fed3efa7Sflorian 	sec = nsec3_do_prove_nodata(env, &flt, ct, qinfo, calc);
1609ae8c6e27Sflorian 	if(sec==sec_status_secure) {
1610ae8c6e27Sflorian 		*nodata = 1;
1611ae8c6e27Sflorian 	} else if(sec == sec_status_insecure) {
1612ae8c6e27Sflorian 		*nodata = 1;
1613ae8c6e27Sflorian 	} else if(secnx == sec_status_insecure) {
1614ae8c6e27Sflorian 		sec = sec_status_insecure;
1615fed3efa7Sflorian 	} else if(sec == sec_status_unchecked) {
1616fed3efa7Sflorian 		return sec_status_unchecked;
1617ae8c6e27Sflorian 	}
1618ae8c6e27Sflorian 	return sec;
1619ae8c6e27Sflorian }
1620