val_nsec3.h revision 292206
1238106Sdes/*
2292206Sdes * validator/val_nsec3.h - validator NSEC3 denial of existence functions.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24266114Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25266114Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26266114Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27266114Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28266114Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29266114Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30266114Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31266114Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32266114Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33266114Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file contains helper functions for the validator module.
40238106Sdes * The functions help with NSEC3 checking, the different NSEC3 proofs
41292206Sdes * for denial of existence, and proofs for presence of types.
42238106Sdes *
43238106Sdes * NSEC3
44238106Sdes *                      1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
45238106Sdes *  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
46238106Sdes * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
47238106Sdes * |   Hash Alg.   |     Flags     |          Iterations           |
48238106Sdes * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49238106Sdes * |  Salt Length  |                     Salt                      /
50238106Sdes * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
51238106Sdes * |  Hash Length  |             Next Hashed Owner Name            /
52238106Sdes * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
53238106Sdes * /                         Type Bit Maps                         /
54238106Sdes * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55238106Sdes *
56238106Sdes * NSEC3PARAM
57238106Sdes *                      1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
58238106Sdes *  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
59238106Sdes * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60238106Sdes * |   Hash Alg.   |     Flags     |          Iterations           |
61238106Sdes * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62238106Sdes * |  Salt Length  |                     Salt                      /
63238106Sdes * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64238106Sdes *
65238106Sdes */
66238106Sdes
67238106Sdes#ifndef VALIDATOR_VAL_NSEC3_H
68238106Sdes#define VALIDATOR_VAL_NSEC3_H
69238106Sdes#include "util/rbtree.h"
70238106Sdes#include "util/data/packed_rrset.h"
71238106Sdesstruct val_env;
72238106Sdesstruct regional;
73238106Sdesstruct module_env;
74238106Sdesstruct ub_packed_rrset_key;
75238106Sdesstruct reply_info;
76238106Sdesstruct query_info;
77238106Sdesstruct key_entry_key;
78266114Sdesstruct sldns_buffer;
79238106Sdes
80238106Sdes/**
81238106Sdes *     0 1 2 3 4 5 6 7
82238106Sdes *    +-+-+-+-+-+-+-+-+
83238106Sdes *    |             |O|
84238106Sdes *    +-+-+-+-+-+-+-+-+
85238106Sdes * The OPT-OUT bit in the NSEC3 flags field.
86238106Sdes * If enabled, there can be zero or more unsigned delegations in the span.
87238106Sdes * If disabled, there are zero unsigned delegations in the span.
88238106Sdes */
89238106Sdes#define NSEC3_OPTOUT	0x01
90238106Sdes/**
91238106Sdes * The unknown flags in the NSEC3 flags field.
92238106Sdes * They must be zero, or the NSEC3 is ignored.
93238106Sdes */
94238106Sdes#define NSEC3_UNKNOWN_FLAGS 0xFE
95238106Sdes
96238106Sdes/** The SHA1 hash algorithm for NSEC3 */
97238106Sdes#define NSEC3_HASH_SHA1	0x01
98238106Sdes
99238106Sdes/**
100238106Sdes * Determine if the set of NSEC3 records provided with a response prove NAME
101238106Sdes * ERROR. This means that the NSEC3s prove a) the closest encloser exists,
102238106Sdes * b) the direct child of the closest encloser towards qname doesn't exist,
103238106Sdes * and c) *.closest encloser does not exist.
104238106Sdes *
105238106Sdes * @param env: module environment with temporary region and buffer.
106238106Sdes * @param ve: validator environment, with iteration count settings.
107238106Sdes * @param list: array of RRsets, some of which are NSEC3s.
108238106Sdes * @param num: number of RRsets in the array to examine.
109238106Sdes * @param qinfo: query that is verified for.
110238106Sdes * @param kkey: key entry that signed the NSEC3s.
111238106Sdes * @return:
112238106Sdes * 	sec_status SECURE of the Name Error is proven by the NSEC3 RRs,
113238106Sdes * 	BOGUS if not, INSECURE if all of the NSEC3s could be validly ignored.
114238106Sdes */
115238106Sdesenum sec_status
116238106Sdesnsec3_prove_nameerror(struct module_env* env, struct val_env* ve,
117238106Sdes	struct ub_packed_rrset_key** list, size_t num,
118238106Sdes	struct query_info* qinfo, struct key_entry_key* kkey);
119238106Sdes
120238106Sdes/**
121238106Sdes * Determine if the NSEC3s provided in a response prove the NOERROR/NODATA
122238106Sdes * status. There are a number of different variants to this:
123238106Sdes *
124238106Sdes * 1) Normal NODATA -- qname is matched to an NSEC3 record, type is not
125238106Sdes * present.
126238106Sdes *
127238106Sdes * 2) ENT NODATA -- because there must be NSEC3 record for
128238106Sdes * empty-non-terminals, this is the same as #1.
129238106Sdes *
130238106Sdes * 3) NSEC3 ownername NODATA -- qname matched an existing, lone NSEC3
131238106Sdes * ownername, but qtype was not NSEC3. NOTE: as of nsec-05, this case no
132238106Sdes * longer exists.
133238106Sdes *
134238106Sdes * 4) Wildcard NODATA -- A wildcard matched the name, but not the type.
135238106Sdes *
136238106Sdes * 5) Opt-In DS NODATA -- the qname is covered by an opt-in span and qtype ==
137238106Sdes * DS. (or maybe some future record with the same parent-side-only property)
138238106Sdes *
139238106Sdes * @param env: module environment with temporary region and buffer.
140238106Sdes * @param ve: validator environment, with iteration count settings.
141238106Sdes * @param list: array of RRsets, some of which are NSEC3s.
142238106Sdes * @param num: number of RRsets in the array to examine.
143238106Sdes * @param qinfo: query that is verified for.
144238106Sdes * @param kkey: key entry that signed the NSEC3s.
145238106Sdes * @return:
146238106Sdes * 	sec_status SECURE of the proposition is proven by the NSEC3 RRs,
147238106Sdes * 	BOGUS if not, INSECURE if all of the NSEC3s could be validly ignored.
148238106Sdes */
149238106Sdesenum sec_status
150238106Sdesnsec3_prove_nodata(struct module_env* env, struct val_env* ve,
151238106Sdes	struct ub_packed_rrset_key** list, size_t num,
152238106Sdes	struct query_info* qinfo, struct key_entry_key* kkey);
153238106Sdes
154238106Sdes
155238106Sdes/**
156238106Sdes * Prove that a positive wildcard match was appropriate (no direct match
157238106Sdes * RRset).
158238106Sdes *
159238106Sdes * @param env: module environment with temporary region and buffer.
160238106Sdes * @param ve: validator environment, with iteration count settings.
161238106Sdes * @param list: array of RRsets, some of which are NSEC3s.
162238106Sdes * @param num: number of RRsets in the array to examine.
163238106Sdes * @param qinfo: query that is verified for.
164238106Sdes * @param kkey: key entry that signed the NSEC3s.
165238106Sdes * @param wc: The purported wildcard that matched. This is the wildcard name
166238106Sdes * 	as *.wildcard.name., with the *. label already removed.
167238106Sdes * @return:
168238106Sdes * 	sec_status SECURE of the proposition is proven by the NSEC3 RRs,
169238106Sdes * 	BOGUS if not, INSECURE if all of the NSEC3s could be validly ignored.
170238106Sdes */
171238106Sdesenum sec_status
172238106Sdesnsec3_prove_wildcard(struct module_env* env, struct val_env* ve,
173238106Sdes	struct ub_packed_rrset_key** list, size_t num,
174238106Sdes	struct query_info* qinfo, struct key_entry_key* kkey, uint8_t* wc);
175238106Sdes
176238106Sdes/**
177238106Sdes * Prove that a DS response either had no DS, or wasn't a delegation point.
178238106Sdes *
179238106Sdes * Fundamentally there are two cases here: normal NODATA and Opt-In NODATA.
180238106Sdes *
181238106Sdes * @param env: module environment with temporary region and buffer.
182238106Sdes * @param ve: validator environment, with iteration count settings.
183238106Sdes * @param list: array of RRsets, some of which are NSEC3s.
184238106Sdes * @param num: number of RRsets in the array to examine.
185238106Sdes * @param qinfo: query that is verified for.
186238106Sdes * @param kkey: key entry that signed the NSEC3s.
187238106Sdes * @param reason: string for bogus result.
188238106Sdes * @return:
189238106Sdes * 	sec_status SECURE of the proposition is proven by the NSEC3 RRs,
190238106Sdes * 	BOGUS if not, INSECURE if all of the NSEC3s could be validly ignored.
191238106Sdes * 	or if there was no DS in an insecure (i.e., opt-in) way,
192238106Sdes * 	INDETERMINATE if it was clear that this wasn't a delegation point.
193238106Sdes */
194238106Sdesenum sec_status
195238106Sdesnsec3_prove_nods(struct module_env* env, struct val_env* ve,
196238106Sdes	struct ub_packed_rrset_key** list, size_t num,
197238106Sdes	struct query_info* qinfo, struct key_entry_key* kkey, char** reason);
198238106Sdes
199238106Sdes/**
200238106Sdes * Prove NXDOMAIN or NODATA.
201238106Sdes *
202238106Sdes * @param env: module environment with temporary region and buffer.
203238106Sdes * @param ve: validator environment, with iteration count settings.
204238106Sdes * @param list: array of RRsets, some of which are NSEC3s.
205238106Sdes * @param num: number of RRsets in the array to examine.
206238106Sdes * @param qinfo: query that is verified for.
207238106Sdes * @param kkey: key entry that signed the NSEC3s.
208238106Sdes * @param nodata: if return value is secure, this indicates if nodata or
209238106Sdes * 	nxdomain was proven.
210238106Sdes * @return:
211238106Sdes * 	sec_status SECURE of the proposition is proven by the NSEC3 RRs,
212238106Sdes * 	BOGUS if not, INSECURE if all of the NSEC3s could be validly ignored.
213238106Sdes */
214238106Sdesenum sec_status
215238106Sdesnsec3_prove_nxornodata(struct module_env* env, struct val_env* ve,
216238106Sdes	struct ub_packed_rrset_key** list, size_t num,
217238106Sdes	struct query_info* qinfo, struct key_entry_key* kkey, int* nodata);
218238106Sdes
219238106Sdes/**
220238106Sdes * The NSEC3 hash result storage.
221238106Sdes * Consists of an rbtree, with these nodes in it.
222238106Sdes * The nodes detail how a set of parameters (from nsec3 rr) plus
223238106Sdes * a dname result in a hash.
224238106Sdes */
225238106Sdesstruct nsec3_cached_hash {
226238106Sdes	/** rbtree node, key is this structure */
227238106Sdes	rbnode_t node;
228238106Sdes	/** where are the parameters for conversion, in this rrset data */
229238106Sdes	struct ub_packed_rrset_key* nsec3;
230238106Sdes	/** where are the parameters for conversion, this RR number in data */
231238106Sdes	int rr;
232238106Sdes	/** the name to convert */
233238106Sdes	uint8_t* dname;
234238106Sdes	/** length of the dname */
235238106Sdes	size_t dname_len;
236238106Sdes	/** the hash result (not base32 encoded) */
237238106Sdes	uint8_t* hash;
238238106Sdes	/** length of hash in bytes */
239238106Sdes	size_t hash_len;
240238106Sdes	/** the hash result in base32 encoding */
241238106Sdes	uint8_t* b32;
242238106Sdes	/** length of base32 encoding (as a label) */
243238106Sdes	size_t b32_len;
244238106Sdes};
245238106Sdes
246238106Sdes/**
247238106Sdes * Rbtree for hash cache comparison function.
248238106Sdes * @param c1: key 1.
249238106Sdes * @param c2: key 2.
250238106Sdes * @return: comparison code, -1, 0, 1, of the keys.
251238106Sdes */
252238106Sdesint nsec3_hash_cmp(const void* c1, const void* c2);
253238106Sdes
254238106Sdes/**
255238106Sdes * Obtain the hash of an owner name.
256238106Sdes * Used internally by the nsec3 proof functions in this file.
257238106Sdes * published to enable unit testing of hash algorithms and cache.
258238106Sdes *
259292206Sdes * @param table: the cache table. Must be initialised at start.
260238106Sdes * @param region: scratch region to use for allocation.
261238106Sdes * 	This region holds the tree, if you wipe the region, reinit the tree.
262238106Sdes * @param buf: temporary buffer.
263238106Sdes * @param nsec3: the rrset with parameters
264238106Sdes * @param rr: rr number from d that has the NSEC3 parameters to hash to.
265238106Sdes * @param dname: name to hash
266238106Sdes * 	This pointer is used inside the tree, assumed region-alloced.
267238106Sdes * @param dname_len: the length of the name.
268238106Sdes * @param hash: the hash node is returned on success.
269238106Sdes * @return:
270238106Sdes * 	1 on success, either from cache or newly hashed hash is returned.
271238106Sdes * 	0 on a malloc failure.
272238106Sdes * 	-1 if the NSEC3 rr was badly formatted (i.e. formerr).
273238106Sdes */
274266114Sdesint nsec3_hash_name(rbtree_t* table, struct regional* region,
275266114Sdes	struct sldns_buffer* buf, struct ub_packed_rrset_key* nsec3, int rr,
276266114Sdes	uint8_t* dname, size_t dname_len, struct nsec3_cached_hash** hash);
277238106Sdes
278238106Sdes/**
279238106Sdes * Get next owner name, converted to base32 encoding and with the
280238106Sdes * zone name (taken from the nsec3 owner name) appended.
281238106Sdes * @param rrset: the NSEC3 rrset.
282238106Sdes * @param r: the rr num of the nsec3 in the rrset.
283238106Sdes * @param buf: buffer to store name in
284238106Sdes * @param max: size of buffer.
285238106Sdes * @return length of name on success. 0 on failure (buffer too short or
286238106Sdes *	bad format nsec3 record).
287238106Sdes */
288238106Sdessize_t nsec3_get_nextowner_b32(struct ub_packed_rrset_key* rrset, int r,
289238106Sdes	uint8_t* buf, size_t max);
290238106Sdes
291238106Sdes/**
292238106Sdes * Convert hash into base32 encoding and with the
293238106Sdes * zone name appended.
294238106Sdes * @param hash: hashed buffer
295238106Sdes * @param hashlen: length of hash
296238106Sdes * @param zone: name of zone
297238106Sdes * @param zonelen: length of zonename.
298238106Sdes * @param buf: buffer to store name in
299238106Sdes * @param max: size of buffer.
300238106Sdes * @return length of name on success. 0 on failure (buffer too short or
301238106Sdes *	bad format nsec3 record).
302238106Sdes */
303238106Sdessize_t nsec3_hash_to_b32(uint8_t* hash, size_t hashlen, uint8_t* zone,
304238106Sdes	size_t zonelen, uint8_t* buf, size_t max);
305238106Sdes
306238106Sdes/**
307238106Sdes * Get NSEC3 parameters out of rr.
308238106Sdes * @param rrset: the NSEC3 rrset.
309238106Sdes * @param r: the rr num of the nsec3 in the rrset.
310238106Sdes * @param algo: nsec3 hash algo.
311238106Sdes * @param iter: iteration count.
312238106Sdes * @param salt: ptr to salt inside rdata.
313238106Sdes * @param saltlen: length of salt.
314238106Sdes * @return 0 if bad formatted, unknown nsec3 hash algo, or unknown flags set.
315238106Sdes */
316238106Sdesint nsec3_get_params(struct ub_packed_rrset_key* rrset, int r,
317238106Sdes	int* algo, size_t* iter, uint8_t** salt, size_t* saltlen);
318238106Sdes
319238106Sdes/**
320238106Sdes * Get NSEC3 hashed in a buffer
321238106Sdes * @param buf: buffer for temp use.
322238106Sdes * @param nm: name to hash
323238106Sdes * @param nmlen: length of nm.
324238106Sdes * @param algo: algo to use, must be known.
325238106Sdes * @param iter: iterations
326238106Sdes * @param salt: salt for nsec3
327238106Sdes * @param saltlen: length of salt.
328238106Sdes * @param res: result of hash stored here.
329238106Sdes * @param max: maximum space for result.
330238106Sdes * @return 0 on failure, otherwise bytelength stored.
331238106Sdes */
332266114Sdessize_t nsec3_get_hashed(struct sldns_buffer* buf, uint8_t* nm, size_t nmlen,
333266114Sdes	int algo, size_t iter, uint8_t* salt, size_t saltlen, uint8_t* res,
334266114Sdes	size_t max);
335238106Sdes
336238106Sdes/**
337238106Sdes * see if NSEC3 RR contains given type
338238106Sdes * @param rrset: NSEC3 rrset
339238106Sdes * @param r: RR in rrset
340238106Sdes * @param type: in host order to check bit for.
341238106Sdes * @return true if bit set, false if not or error.
342238106Sdes */
343238106Sdesint nsec3_has_type(struct ub_packed_rrset_key* rrset, int r, uint16_t type);
344238106Sdes
345238106Sdes/**
346238106Sdes * return if nsec3 RR has the optout flag
347238106Sdes * @param rrset: NSEC3 rrset
348238106Sdes * @param r: RR in rrset
349238106Sdes * @return true if optout, false on error or not optout
350238106Sdes */
351238106Sdesint nsec3_has_optout(struct ub_packed_rrset_key* rrset, int r);
352238106Sdes
353238106Sdes/**
354238106Sdes * Return nsec3 RR next hashed owner name
355238106Sdes * @param rrset: NSEC3 rrset
356238106Sdes * @param r: RR in rrset
357238106Sdes * @param next: ptr into rdata to next owner hash
358238106Sdes * @param nextlen: length of hash.
359238106Sdes * @return false on malformed
360238106Sdes */
361238106Sdesint nsec3_get_nextowner(struct ub_packed_rrset_key* rrset, int r,
362238106Sdes	uint8_t** next, size_t* nextlen);
363238106Sdes
364238106Sdes/**
365238106Sdes * nsec3Covers
366238106Sdes * Given a hash and a candidate NSEC3Record, determine if that NSEC3Record
367238106Sdes * covers the hash. Covers specifically means that the hash is in between
368238106Sdes * the owner and next hashes and does not equal either.
369238106Sdes *
370238106Sdes * @param zone: the zone name.
371238106Sdes * @param hash: the hash of the name
372238106Sdes * @param rrset: the rrset of the NSEC3.
373238106Sdes * @param rr: which rr in the rrset.
374238106Sdes * @param buf: temporary buffer.
375238106Sdes * @return true if covers, false if not.
376238106Sdes */
377238106Sdesint nsec3_covers(uint8_t* zone, struct nsec3_cached_hash* hash,
378266114Sdes	struct ub_packed_rrset_key* rrset, int rr, struct sldns_buffer* buf);
379238106Sdes
380238106Sdes#endif /* VALIDATOR_VAL_NSEC3_H */
381