1 /*
2 * validator/val_nsec.c - validator NSEC 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 NSEC checking, the different NSEC proofs
41 * for denial of existence, and proofs for presence of types.
42 */
43 #include "config.h"
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"
51
52 /** get ttl of rrset */
53 static uint32_t
rrset_get_ttl(struct ub_packed_rrset_key * k)54 rrset_get_ttl(struct ub_packed_rrset_key* k)
55 {
56 struct packed_rrset_data* d = (struct packed_rrset_data*)k->entry.data;
57 return d->ttl;
58 }
59
60 int
nsecbitmap_has_type_rdata(uint8_t * bitmap,size_t len,uint16_t type)61 nsecbitmap_has_type_rdata(uint8_t* bitmap, size_t len, uint16_t type)
62 {
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;
68 uint8_t win, winlen;
69 /* read each of the type bitmap windows and see if the searched
70 * type is amongst it */
71 while(len > 0) {
72 if(len < 3) /* bad window, at least window# winlen bitmap */
73 return 0;
74 win = *bitmap++;
75 winlen = *bitmap++;
76 len -= 2;
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;
83 if(winlen <= mybyte)
84 return 0; /* window too short */
85 return (int)(bitmap[mybyte] & masks[type_low&0x7]);
86 } else {
87 /* not the window we are looking for */
88 bitmap += winlen;
89 len -= winlen;
90 }
91 }
92 /* end of bitmap reached, no type found */
93 return 0;
94 }
95
96 int
nsec_has_type(struct ub_packed_rrset_key * nsec,uint16_t type)97 nsec_has_type(struct ub_packed_rrset_key* nsec, uint16_t type)
98 {
99 struct packed_rrset_data* d = (struct packed_rrset_data*)nsec->
100 entry.data;
101 size_t len;
102 if(!d || d->count == 0 || d->rr_len[0] < 2+1)
103 return 0;
104 len = dname_valid(d->rr_data[0]+2, d->rr_len[0]-2);
105 if(!len)
106 return 0;
107 return nsecbitmap_has_type_rdata(d->rr_data[0]+2+len,
108 d->rr_len[0]-2-len, type);
109 }
110
111 /**
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).
118 */
119 static int
nsec_get_next(struct ub_packed_rrset_key * nsec,uint8_t ** nm,size_t * ln)120 nsec_get_next(struct ub_packed_rrset_key* nsec, uint8_t** nm, size_t* ln)
121 {
122 struct packed_rrset_data* d = (struct packed_rrset_data*)nsec->
123 entry.data;
124 if(!d || d->count == 0 || d->rr_len[0] < 2+1) {
125 *nm = 0;
126 *ln = 0;
127 return 0;
128 }
129 *nm = d->rr_data[0]+2;
130 *ln = dname_valid(*nm, d->rr_len[0]-2);
131 if(!*ln) {
132 *nm = 0;
133 *ln = 0;
134 return 0;
135 }
136 return 1;
137 }
138
139 /**
140 * For an NSEC that matches the DS queried for, check absence of DS type.
141 *
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.
147 */
148 static enum sec_status
val_nsec_proves_no_ds(struct ub_packed_rrset_key * nsec,struct query_info * qinfo)149 val_nsec_proves_no_ds(struct ub_packed_rrset_key* nsec,
150 struct query_info* qinfo)
151 {
152 log_assert(qinfo->qtype == LDNS_RR_TYPE_DS);
153 log_assert(ntohs(nsec->rk.type) == LDNS_RR_TYPE_NSEC);
154
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;
159 }
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;
164 }
165
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;
170 }
171 /* Otherwise, this proves no DS. */
172 return sec_status_secure;
173 }
174
175 /** check security status from cache or verify rrset, returns true if secure */
176 static int
nsec_verify_rrset(struct module_env * env,struct val_env * ve,struct ub_packed_rrset_key * nsec,struct key_entry_key * kkey,char ** reason,sldns_ede_code * reason_bogus,struct module_qstate * qstate,char * reasonbuf,size_t reasonlen)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, char* reasonbuf, size_t reasonlen)
181 {
182 struct packed_rrset_data* d = (struct packed_rrset_data*)
183 nsec->entry.data;
184 int verified = 0;
185 if(!d) return 0;
186 if(d->security == sec_status_secure)
187 return 1;
188 rrset_check_sec_status(env->rrset_cache, nsec, *env->now);
189 if(d->security == sec_status_secure)
190 return 1;
191 d->security = val_verify_rrset_entry(env, ve, nsec, kkey, reason,
192 reason_bogus, LDNS_SECTION_AUTHORITY, qstate, &verified,
193 reasonbuf, reasonlen);
194 if(d->security == sec_status_secure) {
195 rrset_update_sec_status(env->rrset_cache, nsec, *env->now);
196 return 1;
197 }
198 return 0;
199 }
200
201 enum sec_status
val_nsec_prove_nodata_dsreply(struct module_env * env,struct val_env * ve,struct query_info * qinfo,struct reply_info * rep,struct key_entry_key * kkey,time_t * proof_ttl,char ** reason,sldns_ede_code * reason_bogus,struct module_qstate * qstate,char * reasonbuf,size_t reasonlen)202 val_nsec_prove_nodata_dsreply(struct module_env* env, struct val_env* ve,
203 struct query_info* qinfo, struct reply_info* rep,
204 struct key_entry_key* kkey, time_t* proof_ttl, char** reason,
205 sldns_ede_code* reason_bogus, struct module_qstate* qstate,
206 char* reasonbuf, size_t reasonlen)
207 {
208 struct ub_packed_rrset_key* nsec = reply_find_rrset_section_ns(
209 rep, qinfo->qname, qinfo->qname_len, LDNS_RR_TYPE_NSEC,
210 qinfo->qclass);
211 enum sec_status sec;
212 size_t i;
213 uint8_t* wc = NULL, *ce = NULL;
214 int valid_nsec = 0;
215 struct ub_packed_rrset_key* wc_nsec = NULL;
216
217 /* If we have a NSEC at the same name, it must prove one
218 * of two things
219 * --
220 * 1) this is a delegation point and there is no DS
221 * 2) this is not a delegation point */
222 if(nsec) {
223 if(!nsec_verify_rrset(env, ve, nsec, kkey, reason,
224 reason_bogus, qstate, reasonbuf, reasonlen)) {
225 verbose(VERB_ALGO, "NSEC RRset for the "
226 "referral did not verify.");
227 return sec_status_bogus;
228 }
229 /* If the NSEC was a wildcard, the verify rewrites the
230 * owner to '*.zone'. Check the NSEC owner matches. */
231 if(query_dname_compare(nsec->rk.dname, qinfo->qname) == 0) {
232 sec = val_nsec_proves_no_ds(nsec, qinfo);
233 if(sec == sec_status_bogus) {
234 /* something was wrong. */
235 *reason = "NSEC does not prove absence of DS";
236 *reason_bogus = LDNS_EDE_DNSSEC_BOGUS;
237 return sec;
238 } else if(sec == sec_status_insecure) {
239 /* this wasn't a delegation point. */
240 return sec;
241 } else if(sec == sec_status_secure) {
242 /* this proved no DS. */
243 *proof_ttl = ub_packed_rrset_ttl(nsec);
244 return sec;
245 }
246 }
247 /* if unchecked, fall through to next proof */
248 /* For *.closest-encloser NSEC, there is a closer-match
249 * check for the wildcard below. */
250 }
251
252 /* Otherwise, there is no NSEC at qname. This could be an ENT.
253 * (ENT=empty non terminal). If not, this is broken. */
254
255 /* verify NSEC rrsets in auth section */
256 for(i=rep->an_numrrsets; i < rep->an_numrrsets+rep->ns_numrrsets;
257 i++) {
258 if(rep->rrsets[i]->rk.type != htons(LDNS_RR_TYPE_NSEC))
259 continue;
260 if(!nsec_verify_rrset(env, ve, rep->rrsets[i], kkey, reason,
261 reason_bogus, qstate, reasonbuf, reasonlen)) {
262 verbose(VERB_ALGO, "NSEC for empty non-terminal "
263 "did not verify.");
264 *reason = "NSEC for empty non-terminal "
265 "did not verify.";
266 return sec_status_bogus;
267 }
268 if(nsec_proves_nodata(rep->rrsets[i], qinfo, &wc)) {
269 verbose(VERB_ALGO, "NSEC for empty non-terminal "
270 "proved no DS.");
271 *proof_ttl = rrset_get_ttl(rep->rrsets[i]);
272 if(wc && dname_is_wild(rep->rrsets[i]->rk.dname))
273 wc_nsec = rep->rrsets[i];
274 valid_nsec = 1;
275 }
276 if(val_nsec_proves_name_error(rep->rrsets[i], qinfo->qname)) {
277 ce = nsec_closest_encloser(qinfo->qname,
278 rep->rrsets[i]);
279 }
280 }
281 if(wc && !ce)
282 valid_nsec = 0;
283 else if(wc && ce) {
284 /* ce and wc must match */
285 if(query_dname_compare(wc, ce) != 0)
286 valid_nsec = 0;
287 else if(!wc_nsec)
288 valid_nsec = 0;
289 }
290 if(valid_nsec) {
291 if(wc) {
292 /* check if this is a delegation */
293 *reason = "NSEC for wildcard does not prove absence of DS";
294 return val_nsec_proves_no_ds(wc_nsec, qinfo);
295 }
296 /* valid nsec proves empty nonterminal */
297 return sec_status_insecure;
298 }
299
300 /* NSEC proof did not conclusively point to DS or no DS */
301 return sec_status_unchecked;
302 }
303
nsec_proves_nodata(struct ub_packed_rrset_key * nsec,struct query_info * qinfo,uint8_t ** wc)304 int nsec_proves_nodata(struct ub_packed_rrset_key* nsec,
305 struct query_info* qinfo, uint8_t** wc)
306 {
307 log_assert(wc);
308 if(query_dname_compare(nsec->rk.dname, qinfo->qname) != 0) {
309 uint8_t* nm;
310 size_t ln;
311
312 /* empty-non-terminal checking.
313 * Done before wildcard, because this is an exact match,
314 * and would prevent a wildcard from matching. */
315
316 /* If the nsec is proving that qname is an ENT, the nsec owner
317 * will be less than qname, and the next name will be a child
318 * domain of the qname. */
319 if(!nsec_get_next(nsec, &nm, &ln))
320 return 0; /* bad nsec */
321 if(dname_strict_subdomain_c(nm, qinfo->qname) &&
322 dname_canonical_compare(nsec->rk.dname,
323 qinfo->qname) < 0) {
324 return 1; /* proves ENT */
325 }
326
327 /* wildcard checking. */
328
329 /* If this is a wildcard NSEC, make sure that a) it was
330 * possible to have generated qname from the wildcard and
331 * b) the type map does not contain qtype. Note that this
332 * does NOT prove that this wildcard was the applicable
333 * wildcard. */
334 if(dname_is_wild(nsec->rk.dname)) {
335 /* the purported closest encloser. */
336 uint8_t* ce = nsec->rk.dname;
337 size_t ce_len = nsec->rk.dname_len;
338 dname_remove_label(&ce, &ce_len);
339
340 /* The qname must be a strict subdomain of the
341 * closest encloser, for the wildcard to apply
342 */
343 if(dname_strict_subdomain_c(qinfo->qname, ce)) {
344 /* here we have a matching NSEC for the qname,
345 * perform matching NSEC checks */
346 if(nsec_has_type(nsec, LDNS_RR_TYPE_CNAME)) {
347 /* should have gotten the wildcard CNAME */
348 return 0;
349 }
350 if(nsec_has_type(nsec, LDNS_RR_TYPE_NS) &&
351 !nsec_has_type(nsec, LDNS_RR_TYPE_SOA)) {
352 /* wrong parentside (wildcard) NSEC used */
353 return 0;
354 }
355 if(nsec_has_type(nsec, qinfo->qtype)) {
356 return 0;
357 }
358 *wc = ce;
359 return 1;
360 }
361 } else {
362 /* See if the next owner name covers a wildcard
363 * empty non-terminal. */
364 while (dname_canonical_compare(nsec->rk.dname, nm) < 0) {
365 /* wildcard does not apply if qname below
366 * the name that exists under the '*' */
367 if (dname_subdomain_c(qinfo->qname, nm))
368 break;
369 /* but if it is a wildcard and qname is below
370 * it, then the wildcard applies. The wildcard
371 * is an empty nonterminal. nodata proven. */
372 if (dname_is_wild(nm)) {
373 size_t ce_len = ln;
374 uint8_t* ce = nm;
375 dname_remove_label(&ce, &ce_len);
376 if(dname_strict_subdomain_c(qinfo->qname, ce)) {
377 *wc = ce;
378 return 1;
379 }
380 }
381 dname_remove_label(&nm, &ln);
382 }
383 }
384
385 /* Otherwise, this NSEC does not prove ENT and is not a
386 * wildcard, so it does not prove NODATA. */
387 return 0;
388 }
389
390 /* If the qtype exists, then we should have gotten it. */
391 if(nsec_has_type(nsec, qinfo->qtype)) {
392 return 0;
393 }
394
395 /* if the name is a CNAME node, then we should have gotten the CNAME*/
396 if(nsec_has_type(nsec, LDNS_RR_TYPE_CNAME)) {
397 return 0;
398 }
399
400 /* If an NS set exists at this name, and NOT a SOA (so this is a
401 * zone cut, not a zone apex), then we should have gotten a
402 * referral (or we just got the wrong NSEC).
403 * The reverse of this check is used when qtype is DS, since that
404 * must use the NSEC from above the zone cut. */
405 if(qinfo->qtype != LDNS_RR_TYPE_DS &&
406 nsec_has_type(nsec, LDNS_RR_TYPE_NS) &&
407 !nsec_has_type(nsec, LDNS_RR_TYPE_SOA)) {
408 return 0;
409 } else if(qinfo->qtype == LDNS_RR_TYPE_DS &&
410 nsec_has_type(nsec, LDNS_RR_TYPE_SOA) &&
411 !dname_is_root(qinfo->qname)) {
412 return 0;
413 }
414
415 return 1;
416 }
417
418 int
val_nsec_proves_name_error(struct ub_packed_rrset_key * nsec,uint8_t * qname)419 val_nsec_proves_name_error(struct ub_packed_rrset_key* nsec, uint8_t* qname)
420 {
421 uint8_t* owner = nsec->rk.dname;
422 uint8_t* next;
423 size_t nlen;
424 if(!nsec_get_next(nsec, &next, &nlen))
425 return 0;
426
427 /* If NSEC owner == qname, then this NSEC proves that qname exists. */
428 if(query_dname_compare(qname, owner) == 0) {
429 return 0;
430 }
431
432 /* If NSEC is a parent of qname, we need to check the type map
433 * If the parent name has a DNAME or is a delegation point, then
434 * this NSEC is being misused. */
435 if(dname_subdomain_c(qname, owner) &&
436 (nsec_has_type(nsec, LDNS_RR_TYPE_DNAME) ||
437 (nsec_has_type(nsec, LDNS_RR_TYPE_NS)
438 && !nsec_has_type(nsec, LDNS_RR_TYPE_SOA))
439 )) {
440 return 0;
441 }
442
443 if(query_dname_compare(owner, next) == 0) {
444 /* this nsec is the only nsec */
445 /* zone.name NSEC zone.name, disproves everything else */
446 /* but only for subdomains of that zone */
447 if(dname_strict_subdomain_c(qname, next))
448 return 1;
449 }
450 else if(dname_canonical_compare(owner, next) > 0) {
451 /* this is the last nsec, ....(bigger) NSEC zonename(smaller) */
452 /* the names after the last (owner) name do not exist
453 * there are no names before the zone name in the zone
454 * but the qname must be a subdomain of the zone name(next). */
455 if(dname_canonical_compare(owner, qname) < 0 &&
456 dname_strict_subdomain_c(qname, next))
457 return 1;
458 } else {
459 /* regular NSEC, (smaller) NSEC (larger) */
460 if(dname_canonical_compare(owner, qname) < 0 &&
461 dname_canonical_compare(qname, next) < 0) {
462 return 1;
463 }
464 }
465 return 0;
466 }
467
val_nsec_proves_insecuredelegation(struct ub_packed_rrset_key * nsec,struct query_info * qinfo)468 int val_nsec_proves_insecuredelegation(struct ub_packed_rrset_key* nsec,
469 struct query_info* qinfo)
470 {
471 if(nsec_has_type(nsec, LDNS_RR_TYPE_NS) &&
472 !nsec_has_type(nsec, LDNS_RR_TYPE_DS) &&
473 !nsec_has_type(nsec, LDNS_RR_TYPE_SOA)) {
474 /* see if nsec signals an insecure delegation */
475 if(qinfo->qtype == LDNS_RR_TYPE_DS) {
476 /* if type is DS and qname is equal to nsec, then it
477 * is an exact match nsec, result not insecure */
478 if(dname_strict_subdomain_c(qinfo->qname,
479 nsec->rk.dname))
480 return 1;
481 } else {
482 if(dname_subdomain_c(qinfo->qname, nsec->rk.dname))
483 return 1;
484 }
485 }
486 return 0;
487 }
488
489 uint8_t*
nsec_closest_encloser(uint8_t * qname,struct ub_packed_rrset_key * nsec)490 nsec_closest_encloser(uint8_t* qname, struct ub_packed_rrset_key* nsec)
491 {
492 uint8_t* next;
493 size_t nlen;
494 uint8_t* common1, *common2;
495 if(!nsec_get_next(nsec, &next, &nlen))
496 return NULL;
497 /* longest common with owner or next name */
498 common1 = dname_get_shared_topdomain(nsec->rk.dname, qname);
499 common2 = dname_get_shared_topdomain(next, qname);
500 if(dname_count_labels(common1) > dname_count_labels(common2))
501 return common1;
502 return common2;
503 }
504
val_nsec_proves_positive_wildcard(struct ub_packed_rrset_key * nsec,struct query_info * qinf,uint8_t * wc)505 int val_nsec_proves_positive_wildcard(struct ub_packed_rrset_key* nsec,
506 struct query_info* qinf, uint8_t* wc)
507 {
508 uint8_t* ce;
509 /* 1) prove that qname doesn't exist and
510 * 2) that the correct wildcard was used
511 * nsec has been verified already. */
512 if(!val_nsec_proves_name_error(nsec, qinf->qname))
513 return 0;
514 /* check wildcard name */
515 ce = nsec_closest_encloser(qinf->qname, nsec);
516 if(!ce)
517 return 0;
518 if(query_dname_compare(wc, ce) != 0) {
519 return 0;
520 }
521 return 1;
522 }
523
524 int
val_nsec_proves_no_wc(struct ub_packed_rrset_key * nsec,uint8_t * qname,size_t qnamelen)525 val_nsec_proves_no_wc(struct ub_packed_rrset_key* nsec, uint8_t* qname,
526 size_t qnamelen)
527 {
528 /* Determine if a NSEC record proves the non-existence of a
529 * wildcard that could have produced qname. */
530 int labs;
531 uint8_t* ce = nsec_closest_encloser(qname, nsec);
532 uint8_t* strip;
533 size_t striplen;
534 uint8_t buf[LDNS_MAX_DOMAINLEN+3];
535 if(!ce)
536 return 0;
537 /* we can subtract the closest encloser count - since that is the
538 * largest shared topdomain with owner and next NSEC name,
539 * because the NSEC is no proof for names shorter than the owner
540 * and next names. */
541 labs = dname_count_labels(qname) - dname_count_labels(ce);
542
543 if(labs > 0) {
544 /* i is number of labels to strip off qname, prepend * wild */
545 strip = qname;
546 striplen = qnamelen;
547 dname_remove_labels(&strip, &striplen, labs);
548 if(striplen > LDNS_MAX_DOMAINLEN-2)
549 return 0; /* too long to prepend wildcard */
550 buf[0] = 1;
551 buf[1] = (uint8_t)'*';
552 memmove(buf+2, strip, striplen);
553 if(val_nsec_proves_name_error(nsec, buf)) {
554 return 1;
555 }
556 }
557 return 0;
558 }
559