xref: /freebsd/contrib/unbound/validator/val_nsec3.h (revision 5685098846d7f11ad642d9804d94dc7429a7b212)
1b7579f77SDag-Erling Smørgrav /*
205ab2901SDag-Erling Smørgrav  * validator/val_nsec3.h - validator NSEC3 denial of existence functions.
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
39b7579f77SDag-Erling Smørgrav  * This file contains helper functions for the validator module.
40b7579f77SDag-Erling Smørgrav  * The functions help with NSEC3 checking, the different NSEC3 proofs
4105ab2901SDag-Erling Smørgrav  * for denial of existence, and proofs for presence of types.
42b7579f77SDag-Erling Smørgrav  *
43b7579f77SDag-Erling Smørgrav  * NSEC3
44b7579f77SDag-Erling Smørgrav  *                      1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
45b7579f77SDag-Erling Smørgrav  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
46b7579f77SDag-Erling Smørgrav  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47b7579f77SDag-Erling Smørgrav  * |   Hash Alg.   |     Flags     |          Iterations           |
48b7579f77SDag-Erling Smørgrav  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49b7579f77SDag-Erling Smørgrav  * |  Salt Length  |                     Salt                      /
50b7579f77SDag-Erling Smørgrav  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51b7579f77SDag-Erling Smørgrav  * |  Hash Length  |             Next Hashed Owner Name            /
52b7579f77SDag-Erling Smørgrav  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53b7579f77SDag-Erling Smørgrav  * /                         Type Bit Maps                         /
54b7579f77SDag-Erling Smørgrav  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55b7579f77SDag-Erling Smørgrav  *
56b7579f77SDag-Erling Smørgrav  * NSEC3PARAM
57b7579f77SDag-Erling Smørgrav  *                      1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
58b7579f77SDag-Erling Smørgrav  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
59b7579f77SDag-Erling Smørgrav  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60b7579f77SDag-Erling Smørgrav  * |   Hash Alg.   |     Flags     |          Iterations           |
61b7579f77SDag-Erling Smørgrav  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62b7579f77SDag-Erling Smørgrav  * |  Salt Length  |                     Salt                      /
63b7579f77SDag-Erling Smørgrav  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64b7579f77SDag-Erling Smørgrav  *
65b7579f77SDag-Erling Smørgrav  */
66b7579f77SDag-Erling Smørgrav 
67b7579f77SDag-Erling Smørgrav #ifndef VALIDATOR_VAL_NSEC3_H
68b7579f77SDag-Erling Smørgrav #define VALIDATOR_VAL_NSEC3_H
69b7579f77SDag-Erling Smørgrav #include "util/rbtree.h"
70b7579f77SDag-Erling Smørgrav #include "util/data/packed_rrset.h"
71a39a5a69SCy Schubert #include "sldns/rrdef.h"
72b7579f77SDag-Erling Smørgrav struct val_env;
73b7579f77SDag-Erling Smørgrav struct regional;
74b7579f77SDag-Erling Smørgrav struct module_env;
75838e13ceSDag-Erling Smørgrav struct module_qstate;
76b7579f77SDag-Erling Smørgrav struct ub_packed_rrset_key;
77b7579f77SDag-Erling Smørgrav struct reply_info;
78b7579f77SDag-Erling Smørgrav struct query_info;
79b7579f77SDag-Erling Smørgrav struct key_entry_key;
8017d15b25SDag-Erling Smørgrav struct sldns_buffer;
81b7579f77SDag-Erling Smørgrav 
82b7579f77SDag-Erling Smørgrav /**
83b7579f77SDag-Erling Smørgrav  *     0 1 2 3 4 5 6 7
84b7579f77SDag-Erling Smørgrav  *    +-+-+-+-+-+-+-+-+
85b7579f77SDag-Erling Smørgrav  *    |             |O|
86b7579f77SDag-Erling Smørgrav  *    +-+-+-+-+-+-+-+-+
87b7579f77SDag-Erling Smørgrav  * The OPT-OUT bit in the NSEC3 flags field.
88b7579f77SDag-Erling Smørgrav  * If enabled, there can be zero or more unsigned delegations in the span.
89b7579f77SDag-Erling Smørgrav  * If disabled, there are zero unsigned delegations in the span.
90b7579f77SDag-Erling Smørgrav  */
91b7579f77SDag-Erling Smørgrav #define NSEC3_OPTOUT	0x01
92b7579f77SDag-Erling Smørgrav /**
93b7579f77SDag-Erling Smørgrav  * The unknown flags in the NSEC3 flags field.
94b7579f77SDag-Erling Smørgrav  * They must be zero, or the NSEC3 is ignored.
95b7579f77SDag-Erling Smørgrav  */
96b7579f77SDag-Erling Smørgrav #define NSEC3_UNKNOWN_FLAGS 0xFE
97b7579f77SDag-Erling Smørgrav 
98b7579f77SDag-Erling Smørgrav /** The SHA1 hash algorithm for NSEC3 */
99b7579f77SDag-Erling Smørgrav #define NSEC3_HASH_SHA1	0x01
100b7579f77SDag-Erling Smørgrav 
101b7579f77SDag-Erling Smørgrav /**
102b76ef9a7SCy Schubert * Cache table for NSEC3 hashes.
103b76ef9a7SCy Schubert * It keeps a *pointer* to the region its items are allocated.
104b76ef9a7SCy Schubert */
105b76ef9a7SCy Schubert struct nsec3_cache_table {
106b76ef9a7SCy Schubert 	rbtree_type* ct;
107b76ef9a7SCy Schubert 	struct regional* region;
108b76ef9a7SCy Schubert };
109b76ef9a7SCy Schubert 
110b76ef9a7SCy Schubert /**
111b7579f77SDag-Erling Smørgrav  * Determine if the set of NSEC3 records provided with a response prove NAME
112b7579f77SDag-Erling Smørgrav  * ERROR. This means that the NSEC3s prove a) the closest encloser exists,
113b7579f77SDag-Erling Smørgrav  * b) the direct child of the closest encloser towards qname doesn't exist,
114b7579f77SDag-Erling Smørgrav  * and c) *.closest encloser does not exist.
115b7579f77SDag-Erling Smørgrav  *
116b7579f77SDag-Erling Smørgrav  * @param env: module environment with temporary region and buffer.
117b7579f77SDag-Erling Smørgrav  * @param ve: validator environment, with iteration count settings.
118b7579f77SDag-Erling Smørgrav  * @param list: array of RRsets, some of which are NSEC3s.
119b7579f77SDag-Erling Smørgrav  * @param num: number of RRsets in the array to examine.
120b7579f77SDag-Erling Smørgrav  * @param qinfo: query that is verified for.
121b7579f77SDag-Erling Smørgrav  * @param kkey: key entry that signed the NSEC3s.
122b76ef9a7SCy Schubert  * @param ct: cached hashes table.
123b76ef9a7SCy Schubert  * @param calc: current hash calculations.
124b7579f77SDag-Erling Smørgrav  * @return:
125b7579f77SDag-Erling Smørgrav  * 	sec_status SECURE of the Name Error is proven by the NSEC3 RRs,
126b76ef9a7SCy Schubert  * 	BOGUS if not, INSECURE if all of the NSEC3s could be validly ignored,
127b76ef9a7SCy Schubert  * 	UNCHECKED if no more hash calculations are allowed at this point.
128b7579f77SDag-Erling Smørgrav  */
129b7579f77SDag-Erling Smørgrav enum sec_status
130b7579f77SDag-Erling Smørgrav nsec3_prove_nameerror(struct module_env* env, struct val_env* ve,
131b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key** list, size_t num,
132b76ef9a7SCy Schubert 	struct query_info* qinfo, struct key_entry_key* kkey,
133b76ef9a7SCy Schubert 	struct nsec3_cache_table* ct, int* calc);
134b7579f77SDag-Erling Smørgrav 
135b7579f77SDag-Erling Smørgrav /**
136b7579f77SDag-Erling Smørgrav  * Determine if the NSEC3s provided in a response prove the NOERROR/NODATA
137b7579f77SDag-Erling Smørgrav  * status. There are a number of different variants to this:
138b7579f77SDag-Erling Smørgrav  *
139b7579f77SDag-Erling Smørgrav  * 1) Normal NODATA -- qname is matched to an NSEC3 record, type is not
140b7579f77SDag-Erling Smørgrav  * present.
141b7579f77SDag-Erling Smørgrav  *
142b7579f77SDag-Erling Smørgrav  * 2) ENT NODATA -- because there must be NSEC3 record for
143b7579f77SDag-Erling Smørgrav  * empty-non-terminals, this is the same as #1.
144b7579f77SDag-Erling Smørgrav  *
145b7579f77SDag-Erling Smørgrav  * 3) NSEC3 ownername NODATA -- qname matched an existing, lone NSEC3
146b7579f77SDag-Erling Smørgrav  * ownername, but qtype was not NSEC3. NOTE: as of nsec-05, this case no
147b7579f77SDag-Erling Smørgrav  * longer exists.
148b7579f77SDag-Erling Smørgrav  *
149b7579f77SDag-Erling Smørgrav  * 4) Wildcard NODATA -- A wildcard matched the name, but not the type.
150b7579f77SDag-Erling Smørgrav  *
151b7579f77SDag-Erling Smørgrav  * 5) Opt-In DS NODATA -- the qname is covered by an opt-in span and qtype ==
152b7579f77SDag-Erling Smørgrav  * DS. (or maybe some future record with the same parent-side-only property)
153b7579f77SDag-Erling Smørgrav  *
154b7579f77SDag-Erling Smørgrav  * @param env: module environment with temporary region and buffer.
155b7579f77SDag-Erling Smørgrav  * @param ve: validator environment, with iteration count settings.
156b7579f77SDag-Erling Smørgrav  * @param list: array of RRsets, some of which are NSEC3s.
157b7579f77SDag-Erling Smørgrav  * @param num: number of RRsets in the array to examine.
158b7579f77SDag-Erling Smørgrav  * @param qinfo: query that is verified for.
159b7579f77SDag-Erling Smørgrav  * @param kkey: key entry that signed the NSEC3s.
160b76ef9a7SCy Schubert  * @param ct: cached hashes table.
161b76ef9a7SCy Schubert  * @param calc: current hash calculations.
162b7579f77SDag-Erling Smørgrav  * @return:
163b7579f77SDag-Erling Smørgrav  * 	sec_status SECURE of the proposition is proven by the NSEC3 RRs,
164b76ef9a7SCy Schubert  * 	BOGUS if not, INSECURE if all of the NSEC3s could be validly ignored,
165b76ef9a7SCy Schubert  * 	UNCHECKED if no more hash calculations are allowed at this point.
166b7579f77SDag-Erling Smørgrav  */
167b7579f77SDag-Erling Smørgrav enum sec_status
168b7579f77SDag-Erling Smørgrav nsec3_prove_nodata(struct module_env* env, struct val_env* ve,
169b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key** list, size_t num,
170b76ef9a7SCy Schubert 	struct query_info* qinfo, struct key_entry_key* kkey,
171b76ef9a7SCy Schubert 	struct nsec3_cache_table* ct, int* calc);
172b7579f77SDag-Erling Smørgrav 
173b7579f77SDag-Erling Smørgrav /**
174b7579f77SDag-Erling Smørgrav  * Prove that a positive wildcard match was appropriate (no direct match
175b7579f77SDag-Erling Smørgrav  * RRset).
176b7579f77SDag-Erling Smørgrav  *
177b7579f77SDag-Erling Smørgrav  * @param env: module environment with temporary region and buffer.
178b7579f77SDag-Erling Smørgrav  * @param ve: validator environment, with iteration count settings.
179b7579f77SDag-Erling Smørgrav  * @param list: array of RRsets, some of which are NSEC3s.
180b7579f77SDag-Erling Smørgrav  * @param num: number of RRsets in the array to examine.
181b7579f77SDag-Erling Smørgrav  * @param qinfo: query that is verified for.
182b7579f77SDag-Erling Smørgrav  * @param kkey: key entry that signed the NSEC3s.
183b7579f77SDag-Erling Smørgrav  * @param wc: The purported wildcard that matched. This is the wildcard name
184b7579f77SDag-Erling Smørgrav  * 	as *.wildcard.name., with the *. label already removed.
185b76ef9a7SCy Schubert  * @param ct: cached hashes table.
186b76ef9a7SCy Schubert  * @param calc: current hash calculations.
187b7579f77SDag-Erling Smørgrav  * @return:
188b7579f77SDag-Erling Smørgrav  * 	sec_status SECURE of the proposition is proven by the NSEC3 RRs,
189b76ef9a7SCy Schubert  * 	BOGUS if not, INSECURE if all of the NSEC3s could be validly ignored,
190b76ef9a7SCy Schubert  * 	UNCHECKED if no more hash calculations are allowed at this point.
191b7579f77SDag-Erling Smørgrav  */
192b7579f77SDag-Erling Smørgrav enum sec_status
193b7579f77SDag-Erling Smørgrav nsec3_prove_wildcard(struct module_env* env, struct val_env* ve,
194b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key** list, size_t num,
195b76ef9a7SCy Schubert 	struct query_info* qinfo, struct key_entry_key* kkey, uint8_t* wc,
196b76ef9a7SCy Schubert 	struct nsec3_cache_table* ct, int* calc);
197b7579f77SDag-Erling Smørgrav 
198b7579f77SDag-Erling Smørgrav /**
199b7579f77SDag-Erling Smørgrav  * Prove that a DS response either had no DS, or wasn't a delegation point.
200b7579f77SDag-Erling Smørgrav  *
201b7579f77SDag-Erling Smørgrav  * Fundamentally there are two cases here: normal NODATA and Opt-In NODATA.
202b7579f77SDag-Erling Smørgrav  *
203b7579f77SDag-Erling Smørgrav  * @param env: module environment with temporary region and buffer.
204b7579f77SDag-Erling Smørgrav  * @param ve: validator environment, with iteration count settings.
205b7579f77SDag-Erling Smørgrav  * @param list: array of RRsets, some of which are NSEC3s.
206b7579f77SDag-Erling Smørgrav  * @param num: number of RRsets in the array to examine.
207b7579f77SDag-Erling Smørgrav  * @param qinfo: query that is verified for.
208b7579f77SDag-Erling Smørgrav  * @param kkey: key entry that signed the NSEC3s.
209b7579f77SDag-Erling Smørgrav  * @param reason: string for bogus result.
210a39a5a69SCy Schubert  * @param reason_bogus: EDE (RFC8914) code paired with the reason of failure.
211838e13ceSDag-Erling Smørgrav  * @param qstate: qstate with region.
212b76ef9a7SCy Schubert  * @param ct: cached hashes table.
213*56850988SCy Schubert  * @param reasonbuf: buffer to use for fail reason string print.
214*56850988SCy Schubert  * @param reasonlen: length of reasonbuf.
215b7579f77SDag-Erling Smørgrav  * @return:
216b7579f77SDag-Erling Smørgrav  * 	sec_status SECURE of the proposition is proven by the NSEC3 RRs,
217b7579f77SDag-Erling Smørgrav  * 	BOGUS if not, INSECURE if all of the NSEC3s could be validly ignored.
218b7579f77SDag-Erling Smørgrav  * 	or if there was no DS in an insecure (i.e., opt-in) way,
219b76ef9a7SCy Schubert  * 	INDETERMINATE if it was clear that this wasn't a delegation point,
220b76ef9a7SCy Schubert  * 	UNCHECKED if no more hash calculations are allowed at this point.
221b7579f77SDag-Erling Smørgrav  */
222b7579f77SDag-Erling Smørgrav enum sec_status
223b7579f77SDag-Erling Smørgrav nsec3_prove_nods(struct module_env* env, struct val_env* ve,
224b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key** list, size_t num,
225838e13ceSDag-Erling Smørgrav 	struct query_info* qinfo, struct key_entry_key* kkey, char** reason,
226b76ef9a7SCy Schubert 	sldns_ede_code* reason_bogus, struct module_qstate* qstate,
227*56850988SCy Schubert 	struct nsec3_cache_table* ct, char* reasonbuf, size_t reasonlen);
228b7579f77SDag-Erling Smørgrav 
229b7579f77SDag-Erling Smørgrav /**
230b7579f77SDag-Erling Smørgrav  * Prove NXDOMAIN or NODATA.
231b7579f77SDag-Erling Smørgrav  *
232b7579f77SDag-Erling Smørgrav  * @param env: module environment with temporary region and buffer.
233b7579f77SDag-Erling Smørgrav  * @param ve: validator environment, with iteration count settings.
234b7579f77SDag-Erling Smørgrav  * @param list: array of RRsets, some of which are NSEC3s.
235b7579f77SDag-Erling Smørgrav  * @param num: number of RRsets in the array to examine.
236b7579f77SDag-Erling Smørgrav  * @param qinfo: query that is verified for.
237b7579f77SDag-Erling Smørgrav  * @param kkey: key entry that signed the NSEC3s.
238b7579f77SDag-Erling Smørgrav  * @param nodata: if return value is secure, this indicates if nodata or
239b7579f77SDag-Erling Smørgrav  * 	nxdomain was proven.
240b76ef9a7SCy Schubert  * @param ct: cached hashes table.
241b76ef9a7SCy Schubert  * @param calc: current hash calculations.
242b7579f77SDag-Erling Smørgrav  * @return:
243b7579f77SDag-Erling Smørgrav  * 	sec_status SECURE of the proposition is proven by the NSEC3 RRs,
244b76ef9a7SCy Schubert  * 	BOGUS if not, INSECURE if all of the NSEC3s could be validly ignored,
245b76ef9a7SCy Schubert  * 	UNCHECKED if no more hash calculations are allowed at this point.
246b7579f77SDag-Erling Smørgrav  */
247b7579f77SDag-Erling Smørgrav enum sec_status
248b7579f77SDag-Erling Smørgrav nsec3_prove_nxornodata(struct module_env* env, struct val_env* ve,
249b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key** list, size_t num,
250b76ef9a7SCy Schubert 	struct query_info* qinfo, struct key_entry_key* kkey, int* nodata,
251b76ef9a7SCy Schubert 	struct nsec3_cache_table* ct, int* calc);
252b7579f77SDag-Erling Smørgrav 
253b7579f77SDag-Erling Smørgrav /**
254b7579f77SDag-Erling Smørgrav  * The NSEC3 hash result storage.
255b7579f77SDag-Erling Smørgrav  * Consists of an rbtree, with these nodes in it.
256b7579f77SDag-Erling Smørgrav  * The nodes detail how a set of parameters (from nsec3 rr) plus
257b7579f77SDag-Erling Smørgrav  * a dname result in a hash.
258b7579f77SDag-Erling Smørgrav  */
259b7579f77SDag-Erling Smørgrav struct nsec3_cached_hash {
260b7579f77SDag-Erling Smørgrav 	/** rbtree node, key is this structure */
2613005e0a3SDag-Erling Smørgrav 	rbnode_type node;
262b7579f77SDag-Erling Smørgrav 	/** where are the parameters for conversion, in this rrset data */
263b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key* nsec3;
264b7579f77SDag-Erling Smørgrav 	/** where are the parameters for conversion, this RR number in data */
265b7579f77SDag-Erling Smørgrav 	int rr;
266b7579f77SDag-Erling Smørgrav 	/** the name to convert */
267b7579f77SDag-Erling Smørgrav 	uint8_t* dname;
268b7579f77SDag-Erling Smørgrav 	/** length of the dname */
269b7579f77SDag-Erling Smørgrav 	size_t dname_len;
270b7579f77SDag-Erling Smørgrav 	/** the hash result (not base32 encoded) */
271b7579f77SDag-Erling Smørgrav 	uint8_t* hash;
272b7579f77SDag-Erling Smørgrav 	/** length of hash in bytes */
273b7579f77SDag-Erling Smørgrav 	size_t hash_len;
274b7579f77SDag-Erling Smørgrav 	/** the hash result in base32 encoding */
275b7579f77SDag-Erling Smørgrav 	uint8_t* b32;
276b7579f77SDag-Erling Smørgrav 	/** length of base32 encoding (as a label) */
277b7579f77SDag-Erling Smørgrav 	size_t b32_len;
278b7579f77SDag-Erling Smørgrav };
279b7579f77SDag-Erling Smørgrav 
280b7579f77SDag-Erling Smørgrav /**
281b7579f77SDag-Erling Smørgrav  * Rbtree for hash cache comparison function.
282b7579f77SDag-Erling Smørgrav  * @param c1: key 1.
283b7579f77SDag-Erling Smørgrav  * @param c2: key 2.
284b7579f77SDag-Erling Smørgrav  * @return: comparison code, -1, 0, 1, of the keys.
285b7579f77SDag-Erling Smørgrav  */
286b7579f77SDag-Erling Smørgrav int nsec3_hash_cmp(const void* c1, const void* c2);
287b7579f77SDag-Erling Smørgrav 
288b7579f77SDag-Erling Smørgrav /**
289b76ef9a7SCy Schubert  * Initialise the NSEC3 cache table.
290b76ef9a7SCy Schubert  * @param ct: the nsec3 cache table.
291b76ef9a7SCy Schubert  * @param region: the region where allocations for the table will happen.
292b76ef9a7SCy Schubert  * @return true on success, false on malloc error.
293b76ef9a7SCy Schubert  */
294b76ef9a7SCy Schubert int nsec3_cache_table_init(struct nsec3_cache_table* ct, struct regional* region);
295b76ef9a7SCy Schubert 
296b76ef9a7SCy Schubert /**
297b7579f77SDag-Erling Smørgrav  * Obtain the hash of an owner name.
298b7579f77SDag-Erling Smørgrav  * Used internally by the nsec3 proof functions in this file.
299b7579f77SDag-Erling Smørgrav  * published to enable unit testing of hash algorithms and cache.
300b7579f77SDag-Erling Smørgrav  *
30105ab2901SDag-Erling Smørgrav  * @param table: the cache table. Must be initialised at start.
302b7579f77SDag-Erling Smørgrav  * @param region: scratch region to use for allocation.
303b7579f77SDag-Erling Smørgrav  * 	This region holds the tree, if you wipe the region, reinit the tree.
304b7579f77SDag-Erling Smørgrav  * @param buf: temporary buffer.
305b7579f77SDag-Erling Smørgrav  * @param nsec3: the rrset with parameters
306b7579f77SDag-Erling Smørgrav  * @param rr: rr number from d that has the NSEC3 parameters to hash to.
307b7579f77SDag-Erling Smørgrav  * @param dname: name to hash
308b7579f77SDag-Erling Smørgrav  * 	This pointer is used inside the tree, assumed region-alloced.
309b7579f77SDag-Erling Smørgrav  * @param dname_len: the length of the name.
310b7579f77SDag-Erling Smørgrav  * @param hash: the hash node is returned on success.
311b7579f77SDag-Erling Smørgrav  * @return:
312b76ef9a7SCy Schubert  * 	2 on success, hash from cache is returned.
313b76ef9a7SCy Schubert  * 	1 on success, newly computed hash is returned.
314b7579f77SDag-Erling Smørgrav  * 	0 on a malloc failure.
315b7579f77SDag-Erling Smørgrav  * 	-1 if the NSEC3 rr was badly formatted (i.e. formerr).
316b7579f77SDag-Erling Smørgrav  */
3173005e0a3SDag-Erling Smørgrav int nsec3_hash_name(rbtree_type* table, struct regional* region,
31817d15b25SDag-Erling Smørgrav 	struct sldns_buffer* buf, struct ub_packed_rrset_key* nsec3, int rr,
31917d15b25SDag-Erling Smørgrav 	uint8_t* dname, size_t dname_len, struct nsec3_cached_hash** hash);
320b7579f77SDag-Erling Smørgrav 
321b7579f77SDag-Erling Smørgrav /**
322b7579f77SDag-Erling Smørgrav  * Get next owner name, converted to base32 encoding and with the
323b7579f77SDag-Erling Smørgrav  * zone name (taken from the nsec3 owner name) appended.
324b7579f77SDag-Erling Smørgrav  * @param rrset: the NSEC3 rrset.
325b7579f77SDag-Erling Smørgrav  * @param r: the rr num of the nsec3 in the rrset.
326b7579f77SDag-Erling Smørgrav  * @param buf: buffer to store name in
327b7579f77SDag-Erling Smørgrav  * @param max: size of buffer.
328b7579f77SDag-Erling Smørgrav  * @return length of name on success. 0 on failure (buffer too short or
329b7579f77SDag-Erling Smørgrav  *	bad format nsec3 record).
330b7579f77SDag-Erling Smørgrav  */
331b7579f77SDag-Erling Smørgrav size_t nsec3_get_nextowner_b32(struct ub_packed_rrset_key* rrset, int r,
332b7579f77SDag-Erling Smørgrav 	uint8_t* buf, size_t max);
333b7579f77SDag-Erling Smørgrav 
334b7579f77SDag-Erling Smørgrav /**
335b7579f77SDag-Erling Smørgrav  * Convert hash into base32 encoding and with the
336b7579f77SDag-Erling Smørgrav  * zone name appended.
337b7579f77SDag-Erling Smørgrav  * @param hash: hashed buffer
338b7579f77SDag-Erling Smørgrav  * @param hashlen: length of hash
339b7579f77SDag-Erling Smørgrav  * @param zone: name of zone
340b7579f77SDag-Erling Smørgrav  * @param zonelen: length of zonename.
341b7579f77SDag-Erling Smørgrav  * @param buf: buffer to store name in
342b7579f77SDag-Erling Smørgrav  * @param max: size of buffer.
343b7579f77SDag-Erling Smørgrav  * @return length of name on success. 0 on failure (buffer too short or
344b7579f77SDag-Erling Smørgrav  *	bad format nsec3 record).
345b7579f77SDag-Erling Smørgrav  */
346b7579f77SDag-Erling Smørgrav size_t nsec3_hash_to_b32(uint8_t* hash, size_t hashlen, uint8_t* zone,
347b7579f77SDag-Erling Smørgrav 	size_t zonelen, uint8_t* buf, size_t max);
348b7579f77SDag-Erling Smørgrav 
349b7579f77SDag-Erling Smørgrav /**
350b7579f77SDag-Erling Smørgrav  * Get NSEC3 parameters out of rr.
351b7579f77SDag-Erling Smørgrav  * @param rrset: the NSEC3 rrset.
352b7579f77SDag-Erling Smørgrav  * @param r: the rr num of the nsec3 in the rrset.
353b7579f77SDag-Erling Smørgrav  * @param algo: nsec3 hash algo.
354b7579f77SDag-Erling Smørgrav  * @param iter: iteration count.
355b7579f77SDag-Erling Smørgrav  * @param salt: ptr to salt inside rdata.
356b7579f77SDag-Erling Smørgrav  * @param saltlen: length of salt.
357b7579f77SDag-Erling Smørgrav  * @return 0 if bad formatted, unknown nsec3 hash algo, or unknown flags set.
358b7579f77SDag-Erling Smørgrav  */
359b7579f77SDag-Erling Smørgrav int nsec3_get_params(struct ub_packed_rrset_key* rrset, int r,
360b7579f77SDag-Erling Smørgrav 	int* algo, size_t* iter, uint8_t** salt, size_t* saltlen);
361b7579f77SDag-Erling Smørgrav 
362b7579f77SDag-Erling Smørgrav /**
363b7579f77SDag-Erling Smørgrav  * Get NSEC3 hashed in a buffer
364b7579f77SDag-Erling Smørgrav  * @param buf: buffer for temp use.
365b7579f77SDag-Erling Smørgrav  * @param nm: name to hash
366b7579f77SDag-Erling Smørgrav  * @param nmlen: length of nm.
367b7579f77SDag-Erling Smørgrav  * @param algo: algo to use, must be known.
368b7579f77SDag-Erling Smørgrav  * @param iter: iterations
369b7579f77SDag-Erling Smørgrav  * @param salt: salt for nsec3
370b7579f77SDag-Erling Smørgrav  * @param saltlen: length of salt.
371b7579f77SDag-Erling Smørgrav  * @param res: result of hash stored here.
372b7579f77SDag-Erling Smørgrav  * @param max: maximum space for result.
373b7579f77SDag-Erling Smørgrav  * @return 0 on failure, otherwise bytelength stored.
374b7579f77SDag-Erling Smørgrav  */
37517d15b25SDag-Erling Smørgrav size_t nsec3_get_hashed(struct sldns_buffer* buf, uint8_t* nm, size_t nmlen,
37617d15b25SDag-Erling Smørgrav 	int algo, size_t iter, uint8_t* salt, size_t saltlen, uint8_t* res,
37717d15b25SDag-Erling Smørgrav 	size_t max);
378b7579f77SDag-Erling Smørgrav 
379b7579f77SDag-Erling Smørgrav /**
380b7579f77SDag-Erling Smørgrav  * see if NSEC3 RR contains given type
381b7579f77SDag-Erling Smørgrav  * @param rrset: NSEC3 rrset
382b7579f77SDag-Erling Smørgrav  * @param r: RR in rrset
383b7579f77SDag-Erling Smørgrav  * @param type: in host order to check bit for.
384b7579f77SDag-Erling Smørgrav  * @return true if bit set, false if not or error.
385b7579f77SDag-Erling Smørgrav  */
386b7579f77SDag-Erling Smørgrav int nsec3_has_type(struct ub_packed_rrset_key* rrset, int r, uint16_t type);
387b7579f77SDag-Erling Smørgrav 
388b7579f77SDag-Erling Smørgrav /**
389b7579f77SDag-Erling Smørgrav  * return if nsec3 RR has the optout flag
390b7579f77SDag-Erling Smørgrav  * @param rrset: NSEC3 rrset
391b7579f77SDag-Erling Smørgrav  * @param r: RR in rrset
392b7579f77SDag-Erling Smørgrav  * @return true if optout, false on error or not optout
393b7579f77SDag-Erling Smørgrav  */
394b7579f77SDag-Erling Smørgrav int nsec3_has_optout(struct ub_packed_rrset_key* rrset, int r);
395b7579f77SDag-Erling Smørgrav 
396b7579f77SDag-Erling Smørgrav /**
397b7579f77SDag-Erling Smørgrav  * Return nsec3 RR next hashed owner name
398b7579f77SDag-Erling Smørgrav  * @param rrset: NSEC3 rrset
399b7579f77SDag-Erling Smørgrav  * @param r: RR in rrset
400b7579f77SDag-Erling Smørgrav  * @param next: ptr into rdata to next owner hash
401b7579f77SDag-Erling Smørgrav  * @param nextlen: length of hash.
402b7579f77SDag-Erling Smørgrav  * @return false on malformed
403b7579f77SDag-Erling Smørgrav  */
404b7579f77SDag-Erling Smørgrav int nsec3_get_nextowner(struct ub_packed_rrset_key* rrset, int r,
405b7579f77SDag-Erling Smørgrav 	uint8_t** next, size_t* nextlen);
406b7579f77SDag-Erling Smørgrav 
407b7579f77SDag-Erling Smørgrav /**
408b7579f77SDag-Erling Smørgrav  * nsec3Covers
409b7579f77SDag-Erling Smørgrav  * Given a hash and a candidate NSEC3Record, determine if that NSEC3Record
410b7579f77SDag-Erling Smørgrav  * covers the hash. Covers specifically means that the hash is in between
411b7579f77SDag-Erling Smørgrav  * the owner and next hashes and does not equal either.
412b7579f77SDag-Erling Smørgrav  *
413b7579f77SDag-Erling Smørgrav  * @param zone: the zone name.
414b7579f77SDag-Erling Smørgrav  * @param hash: the hash of the name
415b7579f77SDag-Erling Smørgrav  * @param rrset: the rrset of the NSEC3.
416b7579f77SDag-Erling Smørgrav  * @param rr: which rr in the rrset.
417b7579f77SDag-Erling Smørgrav  * @param buf: temporary buffer.
418b7579f77SDag-Erling Smørgrav  * @return true if covers, false if not.
419b7579f77SDag-Erling Smørgrav  */
420b7579f77SDag-Erling Smørgrav int nsec3_covers(uint8_t* zone, struct nsec3_cached_hash* hash,
42117d15b25SDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset, int rr, struct sldns_buffer* buf);
422b7579f77SDag-Erling Smørgrav 
423b7579f77SDag-Erling Smørgrav #endif /* VALIDATOR_VAL_NSEC3_H */
424