2 * validator/val_nsec.c - validator NSEC denial of existence functions.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
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.
39 * This file contains helper functions for the validator module.
40 * The functions help with NSEC checking, the different NSEC proofs
41 * for denial of existence, and proofs for presence of types.
44 #include "validator/val_nsec.h"
45 #include "validator/val_utils.h"
46 #include "util/data/msgreply.h"
47 #include "util/data/dname.h"
48 #include "util/net_help.h"
49 #include "util/module.h"
50 #include "services/cache/rrset.h"
52 /** get ttl of rrset */
54 rrset_get_ttl(struct ub_packed_rrset_key* k)
56 struct packed_rrset_data* d = (struct packed_rrset_data*)k->entry.data;
61 nsecbitmap_has_type_rdata(uint8_t* bitmap, size_t len, uint16_t type)
63 /* Check type present in NSEC typemap with bitmap arg */
64 /* bitmasks for determining type-lowerbits presence */
65 uint8_t masks[8] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
66 uint8_t type_window = type>>8;
67 uint8_t type_low = type&0xff;
69 /* read each of the type bitmap windows and see if the searched
70 * type is amongst it */
72 if(len < 3) /* bad window, at least window# winlen bitmap */
77 if(len < winlen || winlen < 1 || winlen > 32)
78 return 0; /* bad window length */
79 if(win == type_window) {
80 /* search window bitmap for the correct byte */
81 /* mybyte is 0 if we need the first byte */
82 size_t mybyte = type_low>>3;
84 return 0; /* window too short */
85 return (int)(bitmap[mybyte] & masks[type_low&0x7]);
87 /* not the window we are looking for */
92 /* end of bitmap reached, no type found */
97 nsec_has_type(struct ub_packed_rrset_key* nsec, uint16_t type)
99 struct packed_rrset_data* d = (struct packed_rrset_data*)nsec->
102 if(!d || d->count == 0 || d->rr_len[0] < 2+1)
104 len = dname_valid(d->rr_data[0]+2, d->rr_len[0]-2);
107 return nsecbitmap_has_type_rdata(d->rr_data[0]+2+len,
108 d->rr_len[0]-2-len, type);
112 * Get next owner name from nsec record
113 * @param nsec: the nsec RRset.
114 * If there are multiple RRs, then this will only return one of them.
115 * @param nm: the next name is returned.
116 * @param ln: length of nm is returned.
117 * @return false on a bad NSEC RR (too short, malformed dname).
120 nsec_get_next(struct ub_packed_rrset_key* nsec, uint8_t** nm, size_t* ln)
122 struct packed_rrset_data* d = (struct packed_rrset_data*)nsec->
124 if(!d || d->count == 0 || d->rr_len[0] < 2+1) {
129 *nm = d->rr_data[0]+2;
130 *ln = dname_valid(*nm, d->rr_len[0]-2);
140 * For an NSEC that matches the DS queried for, check absence of DS type.
142 * @param nsec: NSEC for proof, must be trusted.
143 * @param qinfo: what is queried for.
144 * @return if secure the nsec proves that no DS is present, or
145 * insecure if it proves it is not a delegation point.
146 * or bogus if something was wrong.
148 static enum sec_status
149 val_nsec_proves_no_ds(struct ub_packed_rrset_key* nsec,
150 struct query_info* qinfo)
152 log_assert(qinfo->qtype == LDNS_RR_TYPE_DS);
153 log_assert(ntohs(nsec->rk.type) == LDNS_RR_TYPE_NSEC);
155 if(nsec_has_type(nsec, LDNS_RR_TYPE_SOA) && qinfo->qname_len != 1) {
156 /* SOA present means that this is the NSEC from the child,
157 * not the parent (so it is the wrong one). */
158 return sec_status_bogus;
160 if(nsec_has_type(nsec, LDNS_RR_TYPE_DS)) {
161 /* DS present means that there should have been a positive
162 * response to the DS query, so there is something wrong. */
163 return sec_status_bogus;
166 if(!nsec_has_type(nsec, LDNS_RR_TYPE_NS)) {
167 /* If there is no NS at this point at all, then this
168 * doesn't prove anything one way or the other. */
169 return sec_status_insecure;
171 /* Otherwise, this proves no DS. */
172 return sec_status_secure;
175 /** check security status from cache or verify rrset, returns true if secure */
177 nsec_verify_rrset(struct module_env* env, struct val_env* ve,
178 struct ub_packed_rrset_key* nsec, struct key_entry_key* kkey,
179 char** reason, sldns_ede_code* reason_bogus,
180 struct module_qstate* qstate)
182 struct packed_rrset_data* d = (struct packed_rrset_data*)
186 if(d->security == sec_status_secure)
188 rrset_check_sec_status(env->rrset_cache, nsec, *env->now);
189 if(d->security == sec_status_secure)
191 d->security = val_verify_rrset_entry(env, ve, nsec, kkey, reason,
192 reason_bogus, LDNS_SECTION_AUTHORITY, qstate, &verified);
193 if(d->security == sec_status_secure) {
194 rrset_update_sec_status(env->rrset_cache, nsec, *env->now);
201 val_nsec_prove_nodata_dsreply(struct module_env* env, struct val_env* ve,
202 struct query_info* qinfo, struct reply_info* rep,
203 struct key_entry_key* kkey, time_t* proof_ttl, char** reason,
204 sldns_ede_code* reason_bogus, struct module_qstate* qstate)
206 struct ub_packed_rrset_key* nsec = reply_find_rrset_section_ns(
207 rep, qinfo->qname, qinfo->qname_len, LDNS_RR_TYPE_NSEC,
211 uint8_t* wc = NULL, *ce = NULL;
213 struct ub_packed_rrset_key* wc_nsec = NULL;
215 /* If we have a NSEC at the same name, it must prove one
218 * 1) this is a delegation point and there is no DS
219 * 2) this is not a delegation point */
221 if(!nsec_verify_rrset(env, ve, nsec, kkey, reason,
222 reason_bogus, qstate)) {
223 verbose(VERB_ALGO, "NSEC RRset for the "
224 "referral did not verify.");
225 return sec_status_bogus;
227 sec = val_nsec_proves_no_ds(nsec, qinfo);
228 if(sec == sec_status_bogus) {
229 /* something was wrong. */
230 *reason = "NSEC does not prove absence of DS";
231 *reason_bogus = LDNS_EDE_DNSSEC_BOGUS;
233 } else if(sec == sec_status_insecure) {
234 /* this wasn't a delegation point. */
236 } else if(sec == sec_status_secure) {
237 /* this proved no DS. */
238 *proof_ttl = ub_packed_rrset_ttl(nsec);
241 /* if unchecked, fall through to next proof */
244 /* Otherwise, there is no NSEC at qname. This could be an ENT.
245 * (ENT=empty non terminal). If not, this is broken. */
247 /* verify NSEC rrsets in auth section */
248 for(i=rep->an_numrrsets; i < rep->an_numrrsets+rep->ns_numrrsets;
250 if(rep->rrsets[i]->rk.type != htons(LDNS_RR_TYPE_NSEC))
252 if(!nsec_verify_rrset(env, ve, rep->rrsets[i], kkey, reason,
253 reason_bogus, qstate)) {
254 verbose(VERB_ALGO, "NSEC for empty non-terminal "
256 *reason = "NSEC for empty non-terminal "
258 return sec_status_bogus;
260 if(nsec_proves_nodata(rep->rrsets[i], qinfo, &wc)) {
261 verbose(VERB_ALGO, "NSEC for empty non-terminal "
263 *proof_ttl = rrset_get_ttl(rep->rrsets[i]);
264 if(wc && dname_is_wild(rep->rrsets[i]->rk.dname))
265 wc_nsec = rep->rrsets[i];
268 if(val_nsec_proves_name_error(rep->rrsets[i], qinfo->qname)) {
269 ce = nsec_closest_encloser(qinfo->qname,
276 /* ce and wc must match */
277 if(query_dname_compare(wc, ce) != 0)
284 /* check if this is a delegation */
285 *reason = "NSEC for wildcard does not prove absence of DS";
286 return val_nsec_proves_no_ds(wc_nsec, qinfo);
288 /* valid nsec proves empty nonterminal */
289 return sec_status_insecure;
292 /* NSEC proof did not conclusively point to DS or no DS */
293 return sec_status_unchecked;
296 int nsec_proves_nodata(struct ub_packed_rrset_key* nsec,
297 struct query_info* qinfo, uint8_t** wc)
300 if(query_dname_compare(nsec->rk.dname, qinfo->qname) != 0) {
304 /* empty-non-terminal checking.
305 * Done before wildcard, because this is an exact match,
306 * and would prevent a wildcard from matching. */
308 /* If the nsec is proving that qname is an ENT, the nsec owner
309 * will be less than qname, and the next name will be a child
310 * domain of the qname. */
311 if(!nsec_get_next(nsec, &nm, &ln))
312 return 0; /* bad nsec */
313 if(dname_strict_subdomain_c(nm, qinfo->qname) &&
314 dname_canonical_compare(nsec->rk.dname,
316 return 1; /* proves ENT */
319 /* wildcard checking. */
321 /* If this is a wildcard NSEC, make sure that a) it was
322 * possible to have generated qname from the wildcard and
323 * b) the type map does not contain qtype. Note that this
324 * does NOT prove that this wildcard was the applicable
326 if(dname_is_wild(nsec->rk.dname)) {
327 /* the purported closest encloser. */
328 uint8_t* ce = nsec->rk.dname;
329 size_t ce_len = nsec->rk.dname_len;
330 dname_remove_label(&ce, &ce_len);
332 /* The qname must be a strict subdomain of the
333 * closest encloser, for the wildcard to apply
335 if(dname_strict_subdomain_c(qinfo->qname, ce)) {
336 /* here we have a matching NSEC for the qname,
337 * perform matching NSEC checks */
338 if(nsec_has_type(nsec, LDNS_RR_TYPE_CNAME)) {
339 /* should have gotten the wildcard CNAME */
342 if(nsec_has_type(nsec, LDNS_RR_TYPE_NS) &&
343 !nsec_has_type(nsec, LDNS_RR_TYPE_SOA)) {
344 /* wrong parentside (wildcard) NSEC used */
347 if(nsec_has_type(nsec, qinfo->qtype)) {
354 /* See if the next owner name covers a wildcard
355 * empty non-terminal. */
356 while (dname_canonical_compare(nsec->rk.dname, nm) < 0) {
357 /* wildcard does not apply if qname below
358 * the name that exists under the '*' */
359 if (dname_subdomain_c(qinfo->qname, nm))
361 /* but if it is a wildcard and qname is below
362 * it, then the wildcard applies. The wildcard
363 * is an empty nonterminal. nodata proven. */
364 if (dname_is_wild(nm)) {
367 dname_remove_label(&ce, &ce_len);
368 if(dname_strict_subdomain_c(qinfo->qname, ce)) {
373 dname_remove_label(&nm, &ln);
377 /* Otherwise, this NSEC does not prove ENT and is not a
378 * wildcard, so it does not prove NODATA. */
382 /* If the qtype exists, then we should have gotten it. */
383 if(nsec_has_type(nsec, qinfo->qtype)) {
387 /* if the name is a CNAME node, then we should have gotten the CNAME*/
388 if(nsec_has_type(nsec, LDNS_RR_TYPE_CNAME)) {
392 /* If an NS set exists at this name, and NOT a SOA (so this is a
393 * zone cut, not a zone apex), then we should have gotten a
394 * referral (or we just got the wrong NSEC).
395 * The reverse of this check is used when qtype is DS, since that
396 * must use the NSEC from above the zone cut. */
397 if(qinfo->qtype != LDNS_RR_TYPE_DS &&
398 nsec_has_type(nsec, LDNS_RR_TYPE_NS) &&
399 !nsec_has_type(nsec, LDNS_RR_TYPE_SOA)) {
401 } else if(qinfo->qtype == LDNS_RR_TYPE_DS &&
402 nsec_has_type(nsec, LDNS_RR_TYPE_SOA) &&
403 !dname_is_root(qinfo->qname)) {
411 val_nsec_proves_name_error(struct ub_packed_rrset_key* nsec, uint8_t* qname)
413 uint8_t* owner = nsec->rk.dname;
416 if(!nsec_get_next(nsec, &next, &nlen))
419 /* If NSEC owner == qname, then this NSEC proves that qname exists. */
420 if(query_dname_compare(qname, owner) == 0) {
424 /* If NSEC is a parent of qname, we need to check the type map
425 * If the parent name has a DNAME or is a delegation point, then
426 * this NSEC is being misused. */
427 if(dname_subdomain_c(qname, owner) &&
428 (nsec_has_type(nsec, LDNS_RR_TYPE_DNAME) ||
429 (nsec_has_type(nsec, LDNS_RR_TYPE_NS)
430 && !nsec_has_type(nsec, LDNS_RR_TYPE_SOA))
435 if(query_dname_compare(owner, next) == 0) {
436 /* this nsec is the only nsec */
437 /* zone.name NSEC zone.name, disproves everything else */
438 /* but only for subdomains of that zone */
439 if(dname_strict_subdomain_c(qname, next))
442 else if(dname_canonical_compare(owner, next) > 0) {
443 /* this is the last nsec, ....(bigger) NSEC zonename(smaller) */
444 /* the names after the last (owner) name do not exist
445 * there are no names before the zone name in the zone
446 * but the qname must be a subdomain of the zone name(next). */
447 if(dname_canonical_compare(owner, qname) < 0 &&
448 dname_strict_subdomain_c(qname, next))
451 /* regular NSEC, (smaller) NSEC (larger) */
452 if(dname_canonical_compare(owner, qname) < 0 &&
453 dname_canonical_compare(qname, next) < 0) {
460 int val_nsec_proves_insecuredelegation(struct ub_packed_rrset_key* nsec,
461 struct query_info* qinfo)
463 if(nsec_has_type(nsec, LDNS_RR_TYPE_NS) &&
464 !nsec_has_type(nsec, LDNS_RR_TYPE_DS) &&
465 !nsec_has_type(nsec, LDNS_RR_TYPE_SOA)) {
466 /* see if nsec signals an insecure delegation */
467 if(qinfo->qtype == LDNS_RR_TYPE_DS) {
468 /* if type is DS and qname is equal to nsec, then it
469 * is an exact match nsec, result not insecure */
470 if(dname_strict_subdomain_c(qinfo->qname,
474 if(dname_subdomain_c(qinfo->qname, nsec->rk.dname))
482 nsec_closest_encloser(uint8_t* qname, struct ub_packed_rrset_key* nsec)
486 uint8_t* common1, *common2;
487 if(!nsec_get_next(nsec, &next, &nlen))
489 /* longest common with owner or next name */
490 common1 = dname_get_shared_topdomain(nsec->rk.dname, qname);
491 common2 = dname_get_shared_topdomain(next, qname);
492 if(dname_count_labels(common1) > dname_count_labels(common2))
497 int val_nsec_proves_positive_wildcard(struct ub_packed_rrset_key* nsec,
498 struct query_info* qinf, uint8_t* wc)
501 /* 1) prove that qname doesn't exist and
502 * 2) that the correct wildcard was used
503 * nsec has been verified already. */
504 if(!val_nsec_proves_name_error(nsec, qinf->qname))
506 /* check wildcard name */
507 ce = nsec_closest_encloser(qinf->qname, nsec);
510 if(query_dname_compare(wc, ce) != 0) {
517 val_nsec_proves_no_wc(struct ub_packed_rrset_key* nsec, uint8_t* qname,
520 /* Determine if a NSEC record proves the non-existence of a
521 * wildcard that could have produced qname. */
523 uint8_t* ce = nsec_closest_encloser(qname, nsec);
526 uint8_t buf[LDNS_MAX_DOMAINLEN+3];
529 /* we can subtract the closest encloser count - since that is the
530 * largest shared topdomain with owner and next NSEC name,
531 * because the NSEC is no proof for names shorter than the owner
533 labs = dname_count_labels(qname) - dname_count_labels(ce);
536 /* i is number of labels to strip off qname, prepend * wild */
539 dname_remove_labels(&strip, &striplen, labs);
540 if(striplen > LDNS_MAX_DOMAINLEN-2)
541 return 0; /* too long to prepend wildcard */
543 buf[1] = (uint8_t)'*';
544 memmove(buf+2, strip, striplen);
545 if(val_nsec_proves_name_error(nsec, buf)) {