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