1 /* 2 * validator/validator.c - secure validator DNS query response module 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 a module that performs validation of DNS queries. 40 * According to RFC 4034. 41 */ 42 #include "config.h" 43 #include <ctype.h> 44 #include "validator/validator.h" 45 #include "validator/val_anchor.h" 46 #include "validator/val_kcache.h" 47 #include "validator/val_kentry.h" 48 #include "validator/val_utils.h" 49 #include "validator/val_nsec.h" 50 #include "validator/val_nsec3.h" 51 #include "validator/val_neg.h" 52 #include "validator/val_sigcrypt.h" 53 #include "validator/autotrust.h" 54 #include "services/cache/dns.h" 55 #include "services/cache/rrset.h" 56 #include "util/data/dname.h" 57 #include "util/module.h" 58 #include "util/log.h" 59 #include "util/net_help.h" 60 #include "util/regional.h" 61 #include "util/config_file.h" 62 #include "util/fptr_wlist.h" 63 #include "sldns/rrdef.h" 64 #include "sldns/wire2str.h" 65 #include "sldns/str2wire.h" 66 67 /** Max number of RRSIGs to validate at once, suspend query for later. */ 68 #define MAX_VALIDATE_AT_ONCE 8 69 /** Max number of validation suspends allowed, error out otherwise. */ 70 #define MAX_VALIDATION_SUSPENDS 16 71 /** Max answer RRsets for qtype ANY that are validated. The lists is 72 * shortened to fit this limit. */ 73 #define MAX_RRSETS_ANY_VALIDATED 24 74 75 /* forward decl for cache response and normal super inform calls of a DS */ 76 static void process_ds_response(struct module_qstate* qstate, 77 struct val_qstate* vq, int id, int rcode, struct dns_msg* msg, 78 struct query_info* qinfo, struct sock_list* origin, int* suspend, 79 struct module_qstate* sub_qstate); 80 81 82 /* Updates the supplied EDE (RFC8914) code selectively so we don't lose 83 * a more specific code */ 84 static void 85 update_reason_bogus(struct reply_info* rep, sldns_ede_code reason_bogus) 86 { 87 if(reason_bogus == LDNS_EDE_NONE) return; 88 if(reason_bogus == LDNS_EDE_DNSSEC_BOGUS 89 && rep->reason_bogus != LDNS_EDE_NONE 90 && rep->reason_bogus != LDNS_EDE_DNSSEC_BOGUS) return; 91 rep->reason_bogus = reason_bogus; 92 } 93 94 95 /** fill up nsec3 key iterations config entry */ 96 static int 97 fill_nsec3_iter(size_t** keysize, size_t** maxiter, char* s, int c) 98 { 99 char* e; 100 int i; 101 *keysize = (size_t*)calloc((size_t)c, sizeof(size_t)); 102 *maxiter = (size_t*)calloc((size_t)c, sizeof(size_t)); 103 if(!*keysize || !*maxiter) { 104 free(*keysize); 105 *keysize = NULL; 106 free(*maxiter); 107 *maxiter = NULL; 108 log_err("out of memory"); 109 return 0; 110 } 111 for(i=0; i<c; i++) { 112 (*keysize)[i] = (size_t)strtol(s, &e, 10); 113 if(s == e) { 114 log_err("cannot parse: %s", s); 115 free(*keysize); 116 *keysize = NULL; 117 free(*maxiter); 118 *maxiter = NULL; 119 return 0; 120 } 121 s = e; 122 (*maxiter)[i] = (size_t)strtol(s, &e, 10); 123 if(s == e) { 124 log_err("cannot parse: %s", s); 125 free(*keysize); 126 *keysize = NULL; 127 free(*maxiter); 128 *maxiter = NULL; 129 return 0; 130 } 131 s = e; 132 if(i>0 && (*keysize)[i-1] >= (*keysize)[i]) { 133 log_err("nsec3 key iterations not ascending: %d %d", 134 (int)(*keysize)[i-1], (int)(*keysize)[i]); 135 free(*keysize); 136 *keysize = NULL; 137 free(*maxiter); 138 *maxiter = NULL; 139 return 0; 140 } 141 verbose(VERB_ALGO, "validator nsec3cfg keysz %d mxiter %d", 142 (int)(*keysize)[i], (int)(*maxiter)[i]); 143 } 144 return 1; 145 } 146 147 int 148 val_env_parse_key_iter(char* val_nsec3_key_iterations, size_t** keysize, 149 size_t** maxiter, int* keyiter_count) 150 { 151 int c; 152 c = cfg_count_numbers(val_nsec3_key_iterations); 153 if(c < 1 || (c&1)) { 154 log_err("validator: unparsable or odd nsec3 key " 155 "iterations: %s", val_nsec3_key_iterations); 156 return 0; 157 } 158 *keyiter_count = c/2; 159 if(!fill_nsec3_iter(keysize, maxiter, val_nsec3_key_iterations, c/2)) { 160 log_err("validator: cannot apply nsec3 key iterations"); 161 return 0; 162 } 163 return 1; 164 } 165 166 void 167 val_env_apply_cfg(struct val_env* val_env, struct config_file* cfg, 168 size_t* keysize, size_t* maxiter, int keyiter_count) 169 { 170 free(val_env->nsec3_keysize); 171 free(val_env->nsec3_maxiter); 172 val_env->nsec3_keysize = keysize; 173 val_env->nsec3_maxiter = maxiter; 174 val_env->nsec3_keyiter_count = keyiter_count; 175 val_env->bogus_ttl = (uint32_t)cfg->bogus_ttl; 176 val_env->date_override = cfg->val_date_override; 177 val_env->skew_min = cfg->val_sig_skew_min; 178 val_env->skew_max = cfg->val_sig_skew_max; 179 val_env->max_restart = cfg->val_max_restart; 180 } 181 182 /** apply config settings to validator */ 183 static int 184 val_apply_cfg(struct module_env* env, struct val_env* val_env, 185 struct config_file* cfg) 186 { 187 size_t* keysize=NULL, *maxiter=NULL; 188 int keyiter_count = 0; 189 if(!env->anchors) 190 env->anchors = anchors_create(); 191 if(!env->anchors) { 192 log_err("out of memory"); 193 return 0; 194 } 195 if (env->key_cache) 196 val_env->kcache = env->key_cache; 197 if(!val_env->kcache) 198 val_env->kcache = key_cache_create(cfg); 199 if(!val_env->kcache) { 200 log_err("out of memory"); 201 return 0; 202 } 203 env->key_cache = val_env->kcache; 204 if(!anchors_apply_cfg(env->anchors, cfg)) { 205 log_err("validator: error in trustanchors config"); 206 return 0; 207 } 208 if(!val_env_parse_key_iter(cfg->val_nsec3_key_iterations, 209 &keysize, &maxiter, &keyiter_count)) { 210 return 0; 211 } 212 val_env_apply_cfg(val_env, cfg, keysize, maxiter, keyiter_count); 213 if (env->neg_cache) 214 val_env->neg_cache = env->neg_cache; 215 if(!val_env->neg_cache) 216 val_env->neg_cache = val_neg_create(cfg, 217 val_env->nsec3_maxiter[val_env->nsec3_keyiter_count-1]); 218 if(!val_env->neg_cache) { 219 log_err("out of memory"); 220 return 0; 221 } 222 env->neg_cache = val_env->neg_cache; 223 return 1; 224 } 225 226 #ifdef USE_ECDSA_EVP_WORKAROUND 227 void ecdsa_evp_workaround_init(void); 228 #endif 229 int 230 val_init(struct module_env* env, int id) 231 { 232 struct val_env* val_env = (struct val_env*)calloc(1, 233 sizeof(struct val_env)); 234 if(!val_env) { 235 log_err("malloc failure"); 236 return 0; 237 } 238 env->modinfo[id] = (void*)val_env; 239 env->need_to_validate = 1; 240 lock_basic_init(&val_env->bogus_lock); 241 lock_protect(&val_env->bogus_lock, &val_env->num_rrset_bogus, 242 sizeof(val_env->num_rrset_bogus)); 243 #ifdef USE_ECDSA_EVP_WORKAROUND 244 ecdsa_evp_workaround_init(); 245 #endif 246 if(!val_apply_cfg(env, val_env, env->cfg)) { 247 log_err("validator: could not apply configuration settings."); 248 return 0; 249 } 250 if(env->cfg->disable_edns_do) { 251 struct trust_anchor* anchor = anchors_find_any_noninsecure( 252 env->anchors); 253 if(anchor) { 254 char b[LDNS_MAX_DOMAINLEN]; 255 dname_str(anchor->name, b); 256 log_warn("validator: disable-edns-do is enabled, but there is a trust anchor for '%s'. Since DNSSEC could not work, the disable-edns-do setting is turned off. Continuing without it.", b); 257 lock_basic_unlock(&anchor->lock); 258 env->cfg->disable_edns_do = 0; 259 } 260 } 261 262 return 1; 263 } 264 265 void 266 val_deinit(struct module_env* env, int id) 267 { 268 struct val_env* val_env; 269 if(!env || !env->modinfo[id]) 270 return; 271 val_env = (struct val_env*)env->modinfo[id]; 272 lock_basic_destroy(&val_env->bogus_lock); 273 anchors_delete(env->anchors); 274 env->anchors = NULL; 275 key_cache_delete(val_env->kcache); 276 env->key_cache = NULL; 277 neg_cache_delete(val_env->neg_cache); 278 env->neg_cache = NULL; 279 free(val_env->nsec3_keysize); 280 free(val_env->nsec3_maxiter); 281 free(val_env); 282 env->modinfo[id] = NULL; 283 } 284 285 /** fill in message structure */ 286 static struct val_qstate* 287 val_new_getmsg(struct module_qstate* qstate, struct val_qstate* vq) 288 { 289 if(!qstate->return_msg || qstate->return_rcode != LDNS_RCODE_NOERROR) { 290 /* create a message to verify */ 291 verbose(VERB_ALGO, "constructing reply for validation"); 292 vq->orig_msg = (struct dns_msg*)regional_alloc(qstate->region, 293 sizeof(struct dns_msg)); 294 if(!vq->orig_msg) 295 return NULL; 296 vq->orig_msg->qinfo = qstate->qinfo; 297 vq->orig_msg->rep = (struct reply_info*)regional_alloc( 298 qstate->region, sizeof(struct reply_info)); 299 if(!vq->orig_msg->rep) 300 return NULL; 301 memset(vq->orig_msg->rep, 0, sizeof(struct reply_info)); 302 vq->orig_msg->rep->flags = (uint16_t)(qstate->return_rcode&0xf) 303 |BIT_QR|BIT_RA|(qstate->query_flags|(BIT_CD|BIT_RD)); 304 vq->orig_msg->rep->qdcount = 1; 305 vq->orig_msg->rep->reason_bogus = LDNS_EDE_NONE; 306 } else { 307 vq->orig_msg = qstate->return_msg; 308 } 309 vq->qchase = qstate->qinfo; 310 /* chase reply will be an edited (sub)set of the orig msg rrset ptrs */ 311 vq->chase_reply = regional_alloc_init(qstate->region, 312 vq->orig_msg->rep, 313 sizeof(struct reply_info) - sizeof(struct rrset_ref)); 314 if(!vq->chase_reply) 315 return NULL; 316 if(vq->orig_msg->rep->rrset_count > RR_COUNT_MAX) 317 return NULL; /* protect against integer overflow */ 318 /* Over allocate (+an_numrrsets) in case we need to put extra DNAME 319 * records for unsigned CNAME repetitions */ 320 vq->chase_reply->rrsets = regional_alloc(qstate->region, 321 sizeof(struct ub_packed_rrset_key*) * 322 (vq->orig_msg->rep->rrset_count 323 + vq->orig_msg->rep->an_numrrsets)); 324 if(!vq->chase_reply->rrsets) 325 return NULL; 326 memmove(vq->chase_reply->rrsets, vq->orig_msg->rep->rrsets, 327 sizeof(struct ub_packed_rrset_key*) * 328 vq->orig_msg->rep->rrset_count); 329 vq->rrset_skip = 0; 330 return vq; 331 } 332 333 /** allocate new validator query state */ 334 static struct val_qstate* 335 val_new(struct module_qstate* qstate, int id) 336 { 337 struct val_qstate* vq = (struct val_qstate*)regional_alloc( 338 qstate->region, sizeof(*vq)); 339 log_assert(!qstate->minfo[id]); 340 if(!vq) 341 return NULL; 342 memset(vq, 0, sizeof(*vq)); 343 qstate->minfo[id] = vq; 344 vq->state = VAL_INIT_STATE; 345 return val_new_getmsg(qstate, vq); 346 } 347 348 /** reset validator query state for query restart */ 349 static void 350 val_restart(struct val_qstate* vq) 351 { 352 struct comm_timer* temp_timer; 353 int restart_count, num_validation_attempts, num_hash_attempts; 354 if(!vq) return; 355 temp_timer = vq->suspend_timer; 356 restart_count = vq->restart_count+1; 357 num_validation_attempts = vq->num_validation_attempts; 358 num_hash_attempts = vq->num_hash_attempts; 359 memset(vq, 0, sizeof(*vq)); 360 vq->suspend_timer = temp_timer; 361 vq->restart_count = restart_count; 362 vq->num_validation_attempts = num_validation_attempts; 363 vq->num_hash_attempts = num_hash_attempts; 364 vq->state = VAL_INIT_STATE; 365 } 366 367 /** 368 * Exit validation with an error status 369 * 370 * @param qstate: query state 371 * @param id: validator id. 372 * @return false, for use by caller to return to stop processing. 373 */ 374 static int 375 val_error(struct module_qstate* qstate, int id) 376 { 377 qstate->ext_state[id] = module_error; 378 qstate->return_rcode = LDNS_RCODE_SERVFAIL; 379 return 0; 380 } 381 382 /** 383 * Check to see if a given response needs to go through the validation 384 * process. Typical reasons for this routine to return false are: CD bit was 385 * on in the original request, or the response is a kind of message that 386 * is unvalidatable (i.e., SERVFAIL, REFUSED, etc.) 387 * 388 * @param qstate: query state. 389 * @param ret_rc: rcode for this message (if noerror - examine ret_msg). 390 * @param ret_msg: return msg, can be NULL; look at rcode instead. 391 * @return true if the response could use validation (although this does not 392 * mean we can actually validate this response). 393 */ 394 static int 395 needs_validation(struct module_qstate* qstate, int ret_rc, 396 struct dns_msg* ret_msg) 397 { 398 int rcode; 399 400 /* If the CD bit is on in the original request, then you could think 401 * that we don't bother to validate anything. 402 * But this is signalled internally with the valrec flag. 403 * User queries are validated with BIT_CD to make our cache clean 404 * so that bogus messages get retried by the upstream also for 405 * downstream validators that set BIT_CD. 406 * For DNS64 bit_cd signals no dns64 processing, but we want to 407 * provide validation there too */ 408 /* 409 if((qstate->query_flags & BIT_CD)) { 410 verbose(VERB_ALGO, "not validating response due to CD bit"); 411 return 0; 412 } 413 */ 414 if(qstate->is_valrec) { 415 verbose(VERB_ALGO, "not validating response, is valrec" 416 "(validation recursion lookup)"); 417 return 0; 418 } 419 420 if(ret_rc != LDNS_RCODE_NOERROR || !ret_msg) 421 rcode = ret_rc; 422 else rcode = (int)FLAGS_GET_RCODE(ret_msg->rep->flags); 423 424 if(rcode != LDNS_RCODE_NOERROR && rcode != LDNS_RCODE_NXDOMAIN) { 425 if(verbosity >= VERB_ALGO) { 426 char rc[16]; 427 rc[0]=0; 428 (void)sldns_wire2str_rcode_buf(rcode, rc, sizeof(rc)); 429 verbose(VERB_ALGO, "cannot validate non-answer, rcode %s", rc); 430 } 431 return 0; 432 } 433 434 /* cannot validate positive RRSIG response. (negatives can) */ 435 if(qstate->qinfo.qtype == LDNS_RR_TYPE_RRSIG && 436 rcode == LDNS_RCODE_NOERROR && ret_msg && 437 ret_msg->rep->an_numrrsets > 0) { 438 verbose(VERB_ALGO, "cannot validate RRSIG, no sigs on sigs."); 439 return 0; 440 } 441 return 1; 442 } 443 444 /** 445 * Check to see if the response has already been validated. 446 * @param ret_msg: return msg, can be NULL 447 * @return true if the response has already been validated 448 */ 449 static int 450 already_validated(struct dns_msg* ret_msg) 451 { 452 /* validate unchecked, and re-validate bogus messages */ 453 if (ret_msg && ret_msg->rep->security > sec_status_bogus) 454 { 455 verbose(VERB_ALGO, "response has already been validated: %s", 456 sec_status_to_string(ret_msg->rep->security)); 457 return 1; 458 } 459 return 0; 460 } 461 462 /** If it is possible to restart the validation state */ 463 static int 464 val_can_restart(struct module_qstate* qstate, struct val_qstate* vq, 465 struct val_env* ve) 466 { 467 /* For validation failures that are limits exceeded on the amount 468 * of work that the DNSSEC validator is willing to do, the restart 469 * is not allowed. A restart would increase the amount of effort 470 * spent even further. */ 471 if(vq->restart_count < ve->max_restart && 472 vq->num_validation_attempts <= qstate->env->cfg->val_validation_attempts && 473 vq->num_hash_attempts <= qstate->env->cfg->val_hash_attempts && 474 !vq->num_nsec_attempts_exceeded) 475 return 1; 476 (void)qstate; 477 return 0; 478 } 479 480 /** 481 * Generate a request for DNS data. 482 * 483 * @param qstate: query state that is the parent. 484 * @param id: module id. 485 * @param name: what name to query for. 486 * @param namelen: length of name. 487 * @param qtype: query type. 488 * @param qclass: query class. 489 * @param flags: additional flags, such as the CD bit (BIT_CD), or 0. 490 * @param newq: If the subquery is newly created, it is returned, 491 * otherwise NULL is returned 492 * @param detached: true if this qstate should not attach to the subquery 493 * @return false on alloc failure. 494 */ 495 static int 496 generate_request(struct module_qstate* qstate, int id, uint8_t* name, 497 size_t namelen, uint16_t qtype, uint16_t qclass, uint16_t flags, 498 struct module_qstate** newq, int detached) 499 { 500 struct val_qstate* vq = (struct val_qstate*)qstate->minfo[id]; 501 struct query_info ask; 502 int valrec; 503 ask.qname = name; 504 ask.qname_len = namelen; 505 ask.qtype = qtype; 506 ask.qclass = qclass; 507 ask.local_alias = NULL; 508 log_query_info(VERB_ALGO, "generate request", &ask); 509 /* enable valrec flag to avoid recursion to the same validation 510 * routine, this lookup is simply a lookup. */ 511 valrec = 1; 512 513 fptr_ok(fptr_whitelist_modenv_detect_cycle(qstate->env->detect_cycle)); 514 if((*qstate->env->detect_cycle)(qstate, &ask, 515 (uint16_t)(BIT_RD|flags), 0, valrec)) { 516 verbose(VERB_ALGO, "Could not generate request: cycle detected"); 517 return 0; 518 } 519 520 if(detached) { 521 struct mesh_state* sub = NULL; 522 fptr_ok(fptr_whitelist_modenv_add_sub( 523 qstate->env->add_sub)); 524 if(!(*qstate->env->add_sub)(qstate, &ask, NULL, 525 (uint16_t)(BIT_RD|flags), 0, valrec, newq, &sub)){ 526 log_err("Could not generate request: out of memory"); 527 return 0; 528 } 529 } 530 else { 531 fptr_ok(fptr_whitelist_modenv_attach_sub( 532 qstate->env->attach_sub)); 533 if(!(*qstate->env->attach_sub)(qstate, &ask, NULL, 534 (uint16_t)(BIT_RD|flags), 0, valrec, newq)){ 535 log_err("Could not generate request: out of memory"); 536 return 0; 537 } 538 } 539 /* newq; validator does not need state created for that 540 * query, and its a 'normal' for iterator as well */ 541 if(*newq) { 542 /* add our blacklist to the query blacklist */ 543 sock_list_merge(&(*newq)->blacklist, (*newq)->region, 544 vq->chain_blacklist); 545 /* start its global quota counter where this one is. */ 546 if(qstate->global_quota_reached > 547 (*newq)->global_quota_reached) { 548 (*newq)->global_quota_started = 549 qstate->global_quota_reached; 550 (*newq)->global_quota_reached = 551 qstate->global_quota_reached; 552 } 553 } 554 qstate->ext_state[id] = module_wait_subquery; 555 return 1; 556 } 557 558 /** 559 * Generate, send and detach key tag signaling query. 560 * 561 * @param qstate: query state. 562 * @param id: module id. 563 * @param ta: trust anchor, locked. 564 * @return false on a processing error. 565 */ 566 static int 567 generate_keytag_query(struct module_qstate* qstate, int id, 568 struct trust_anchor* ta) 569 { 570 /* 3 bytes for "_ta", 5 bytes per tag (4 bytes + "-") */ 571 #define MAX_LABEL_TAGS (LDNS_MAX_LABELLEN-3)/5 572 size_t i, numtag; 573 uint16_t tags[MAX_LABEL_TAGS]; 574 char tagstr[LDNS_MAX_LABELLEN+1] = "_ta"; /* +1 for NULL byte */ 575 size_t tagstr_left = sizeof(tagstr) - strlen(tagstr); 576 char* tagstr_pos = tagstr + strlen(tagstr); 577 uint8_t dnamebuf[LDNS_MAX_DOMAINLEN+1]; /* +1 for label length byte */ 578 size_t dnamebuf_len = sizeof(dnamebuf); 579 uint8_t* keytagdname; 580 struct module_qstate* newq = NULL; 581 enum module_ext_state ext_state = qstate->ext_state[id]; 582 583 numtag = anchor_list_keytags(ta, tags, MAX_LABEL_TAGS); 584 if(numtag == 0) 585 return 0; 586 587 for(i=0; i<numtag; i++) { 588 /* Buffer can't overflow; numtag is limited to tags that fit in 589 * the buffer. */ 590 snprintf(tagstr_pos, tagstr_left, "-%04x", (unsigned)tags[i]); 591 tagstr_left -= strlen(tagstr_pos); 592 tagstr_pos += strlen(tagstr_pos); 593 } 594 595 sldns_str2wire_dname_buf_origin(tagstr, dnamebuf, &dnamebuf_len, 596 ta->name, ta->namelen); 597 if(!(keytagdname = (uint8_t*)regional_alloc_init(qstate->region, 598 dnamebuf, dnamebuf_len))) { 599 log_err("could not generate key tag query: out of memory"); 600 return 0; 601 } 602 603 log_nametypeclass(VERB_OPS, "generate keytag query", keytagdname, 604 LDNS_RR_TYPE_NULL, ta->dclass); 605 if(!generate_request(qstate, id, keytagdname, dnamebuf_len, 606 LDNS_RR_TYPE_NULL, ta->dclass, 0, &newq, 1)) { 607 verbose(VERB_ALGO, "failed to generate key tag signaling request"); 608 return 0; 609 } 610 611 /* Not interested in subquery response. Restore the ext_state, 612 * that might be changed by generate_request() */ 613 qstate->ext_state[id] = ext_state; 614 615 return 1; 616 } 617 618 /** 619 * Get keytag as uint16_t from string 620 * 621 * @param start: start of string containing keytag 622 * @param keytag: pointer where to store the extracted keytag 623 * @return: 1 if keytag was extracted, else 0. 624 */ 625 static int 626 sentinel_get_keytag(char* start, uint16_t* keytag) { 627 char* keytag_str; 628 char* e = NULL; 629 keytag_str = calloc(1, SENTINEL_KEYTAG_LEN + 1 /* null byte */); 630 if(!keytag_str) 631 return 0; 632 memmove(keytag_str, start, SENTINEL_KEYTAG_LEN); 633 keytag_str[SENTINEL_KEYTAG_LEN] = '\0'; 634 *keytag = (uint16_t)strtol(keytag_str, &e, 10); 635 if(!e || *e != '\0') { 636 free(keytag_str); 637 return 0; 638 } 639 free(keytag_str); 640 return 1; 641 } 642 643 /** 644 * Prime trust anchor for use. 645 * Generate and dispatch a priming query for the given trust anchor. 646 * The trust anchor can be DNSKEY or DS and does not have to be signed. 647 * 648 * @param qstate: query state. 649 * @param vq: validator query state. 650 * @param id: module id. 651 * @param toprime: what to prime. 652 * @return false on a processing error. 653 */ 654 static int 655 prime_trust_anchor(struct module_qstate* qstate, struct val_qstate* vq, 656 int id, struct trust_anchor* toprime) 657 { 658 struct module_qstate* newq = NULL; 659 int ret = generate_request(qstate, id, toprime->name, toprime->namelen, 660 LDNS_RR_TYPE_DNSKEY, toprime->dclass, BIT_CD, &newq, 0); 661 662 if(newq && qstate->env->cfg->trust_anchor_signaling && 663 !generate_keytag_query(qstate, id, toprime)) { 664 verbose(VERB_ALGO, "keytag signaling query failed"); 665 return 0; 666 } 667 668 if(!ret) { 669 verbose(VERB_ALGO, "Could not prime trust anchor"); 670 return 0; 671 } 672 /* ignore newq; validator does not need state created for that 673 * query, and its a 'normal' for iterator as well */ 674 vq->wait_prime_ta = 1; /* to elicit PRIME_RESP_STATE processing 675 from the validator inform_super() routine */ 676 /* store trust anchor name for later lookup when prime returns */ 677 vq->trust_anchor_name = regional_alloc_init(qstate->region, 678 toprime->name, toprime->namelen); 679 vq->trust_anchor_len = toprime->namelen; 680 vq->trust_anchor_labs = toprime->namelabs; 681 if(!vq->trust_anchor_name) { 682 log_err("Could not prime trust anchor: out of memory"); 683 return 0; 684 } 685 return 1; 686 } 687 688 /** 689 * Validate if the ANSWER and AUTHORITY sections contain valid rrsets. 690 * They must be validly signed with the given key. 691 * Tries to validate ADDITIONAL rrsets as well, but only to check them. 692 * Allows unsigned CNAME after a DNAME that expands the DNAME. 693 * 694 * Note that by the time this method is called, the process of finding the 695 * trusted DNSKEY rrset that signs this response must already have been 696 * completed. 697 * 698 * @param qstate: query state. 699 * @param vq: validator query state. 700 * @param env: module env for verify. 701 * @param ve: validator env for verify. 702 * @param chase_reply: answer to validate. 703 * @param key_entry: the key entry, which is trusted, and which matches 704 * the signer of the answer. The key entry isgood(). 705 * @param suspend: returned true if the task takes too long and needs to 706 * suspend to continue the effort later. 707 * @return false if any of the rrsets in the an or ns sections of the message 708 * fail to verify. The message is then set to bogus. 709 */ 710 static int 711 validate_msg_signatures(struct module_qstate* qstate, struct val_qstate* vq, 712 struct module_env* env, struct val_env* ve, 713 struct reply_info* chase_reply, struct key_entry_key* key_entry, 714 int* suspend) 715 { 716 uint8_t* sname; 717 size_t i, slen; 718 struct ub_packed_rrset_key* s; 719 enum sec_status sec; 720 int num_verifies = 0, verified, have_state = 0; 721 char reasonbuf[256]; 722 char* reason = NULL; 723 sldns_ede_code reason_bogus = LDNS_EDE_DNSSEC_BOGUS; 724 *suspend = 0; 725 if(vq->msg_signatures_state) { 726 /* Pick up the state, and reset it, may not be needed now. */ 727 vq->msg_signatures_state = 0; 728 have_state = 1; 729 } 730 731 /* validate the ANSWER section */ 732 for(i=0; i<chase_reply->an_numrrsets; i++) { 733 if(have_state && i <= vq->msg_signatures_index) 734 continue; 735 s = chase_reply->rrsets[i]; 736 /* Skip the CNAME following a (validated) DNAME. 737 * Because of the normalization routines in the iterator, 738 * there will always be an unsigned CNAME following a DNAME 739 * (unless qtype=DNAME in the answer part). */ 740 if(i>0 && ntohs(chase_reply->rrsets[i-1]->rk.type) == 741 LDNS_RR_TYPE_DNAME && 742 ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME && 743 ((struct packed_rrset_data*)chase_reply->rrsets[i-1]->entry.data)->security == sec_status_secure && 744 dname_strict_subdomain_c(s->rk.dname, chase_reply->rrsets[i-1]->rk.dname) 745 ) { 746 /* Check that the CNAME target matches the DNAME 747 * derivation. Zone changes during the redirection 748 * lookups or looped DNAMEs can have such a CNAME. */ 749 uint8_t expected_target[LDNS_MAX_DOMAINLEN]; 750 uint8_t* cname_target = NULL; 751 size_t cname_target_len = 0; 752 get_cname_target(s, &cname_target, &cname_target_len); 753 if(!cname_target || 754 !derive_cname_from_dname(s, /* CNAME RRset */ 755 chase_reply->rrsets[i-1], /* DNAME RRset */ 756 expected_target, /* Output buffer */ 757 sizeof(expected_target))) { 758 verbose(VERB_ALGO, "DNAME CNAME derivation failed"); 759 errinf_ede(qstate, "DNAME CNAME derivation failed", reason_bogus); 760 errinf_origin(qstate, qstate->reply_origin); 761 chase_reply->security = sec_status_bogus; 762 update_reason_bogus(chase_reply, reason_bogus); 763 return 0; 764 } 765 if(query_dname_compare(cname_target, expected_target) != 0) { 766 verbose(VERB_ALGO, "CNAME target mismatch: not synthesized from DNAME"); 767 errinf_ede(qstate, "CNAME target mismatch: not synthesized from DNAME", reason_bogus); 768 errinf_dname(qstate, ", for", s->rk.dname); 769 errinf_dname(qstate, "CNAME", cname_target); 770 errinf(qstate, ","); 771 errinf_origin(qstate, qstate->reply_origin); 772 chase_reply->security = sec_status_bogus; 773 update_reason_bogus(chase_reply, reason_bogus); 774 return 0; 775 } 776 777 /* CNAME was synthesized by our own iterator */ 778 /* since the DNAME verified, mark the CNAME as secure */ 779 ((struct packed_rrset_data*)s->entry.data)->security = 780 sec_status_secure; 781 ((struct packed_rrset_data*)s->entry.data)->trust = 782 rrset_trust_validated; 783 continue; 784 } 785 786 /* Verify the answer rrset */ 787 sec = val_verify_rrset_entry(env, ve, s, key_entry, &reason, 788 &reason_bogus, LDNS_SECTION_ANSWER, qstate, vq, 789 &verified, reasonbuf, sizeof(reasonbuf)); 790 /* If the (answer) rrset failed to validate, then this 791 * message is BAD. */ 792 if(sec != sec_status_secure) { 793 log_nametypeclass(VERB_QUERY, "validator: response " 794 "has failed ANSWER rrset:", s->rk.dname, 795 ntohs(s->rk.type), ntohs(s->rk.rrset_class)); 796 errinf_ede(qstate, reason, reason_bogus); 797 if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) 798 errinf(qstate, "for CNAME"); 799 else if(ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME) 800 errinf(qstate, "for DNAME"); 801 errinf_origin(qstate, qstate->reply_origin); 802 chase_reply->security = sec_status_bogus; 803 update_reason_bogus(chase_reply, reason_bogus); 804 805 return 0; 806 } 807 808 num_verifies += verified; 809 if(num_verifies > MAX_VALIDATE_AT_ONCE && 810 i+1 < (env->cfg->val_clean_additional? 811 chase_reply->an_numrrsets+chase_reply->ns_numrrsets: 812 chase_reply->rrset_count)) { 813 /* If the number of RRSIGs exceeds the maximum in 814 * one go, suspend. Only suspend if there is a next 815 * rrset to verify, i+1<loopmax. Store where to 816 * continue later. */ 817 *suspend = 1; 818 vq->msg_signatures_state = 1; 819 vq->msg_signatures_index = i; 820 verbose(VERB_ALGO, "msg signature validation " 821 "suspended"); 822 return 0; 823 } 824 } 825 826 /* validate the AUTHORITY section */ 827 for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+ 828 chase_reply->ns_numrrsets; i++) { 829 if(have_state && i <= vq->msg_signatures_index) 830 continue; 831 s = chase_reply->rrsets[i]; 832 sec = val_verify_rrset_entry(env, ve, s, key_entry, &reason, 833 &reason_bogus, LDNS_SECTION_AUTHORITY, qstate, vq, 834 &verified, reasonbuf, sizeof(reasonbuf)); 835 /* If anything in the authority section fails to be secure, 836 * we have a bad message. */ 837 if(sec != sec_status_secure) { 838 log_nametypeclass(VERB_QUERY, "validator: response " 839 "has failed AUTHORITY rrset:", s->rk.dname, 840 ntohs(s->rk.type), ntohs(s->rk.rrset_class)); 841 errinf_ede(qstate, reason, reason_bogus); 842 errinf_origin(qstate, qstate->reply_origin); 843 errinf_rrset(qstate, s); 844 chase_reply->security = sec_status_bogus; 845 update_reason_bogus(chase_reply, reason_bogus); 846 return 0; 847 } 848 num_verifies += verified; 849 if(num_verifies > MAX_VALIDATE_AT_ONCE && 850 i+1 < (env->cfg->val_clean_additional? 851 chase_reply->an_numrrsets+chase_reply->ns_numrrsets: 852 chase_reply->rrset_count)) { 853 *suspend = 1; 854 vq->msg_signatures_state = 1; 855 vq->msg_signatures_index = i; 856 verbose(VERB_ALGO, "msg signature validation " 857 "suspended"); 858 return 0; 859 } 860 } 861 862 /* If set, the validator should clean the additional section of 863 * secure messages. */ 864 if(!env->cfg->val_clean_additional) 865 return 1; 866 /* attempt to validate the ADDITIONAL section rrsets */ 867 for(i=chase_reply->an_numrrsets+chase_reply->ns_numrrsets; 868 i<chase_reply->rrset_count; i++) { 869 if(have_state && i <= vq->msg_signatures_index) 870 continue; 871 s = chase_reply->rrsets[i]; 872 /* only validate rrs that have signatures with the key */ 873 /* leave others unchecked, those get removed later on too */ 874 val_find_rrset_signer(s, &sname, &slen); 875 876 verified = 0; 877 if(sname && query_dname_compare(sname, key_entry->name)==0) 878 (void)val_verify_rrset_entry(env, ve, s, key_entry, 879 &reason, NULL, LDNS_SECTION_ADDITIONAL, qstate, 880 vq, &verified, reasonbuf, sizeof(reasonbuf)); 881 /* the additional section can fail to be secure, 882 * it is optional, check signature in case we need 883 * to clean the additional section later. */ 884 num_verifies += verified; 885 if(num_verifies > MAX_VALIDATE_AT_ONCE && 886 i+1 < chase_reply->rrset_count) { 887 *suspend = 1; 888 vq->msg_signatures_state = 1; 889 vq->msg_signatures_index = i; 890 verbose(VERB_ALGO, "msg signature validation " 891 "suspended"); 892 return 0; 893 } 894 } 895 896 return 1; 897 } 898 899 void 900 validate_suspend_timer_cb(void* arg) 901 { 902 struct module_qstate* qstate = (struct module_qstate*)arg; 903 verbose(VERB_ALGO, "validate_suspend timer, continue"); 904 mesh_run(qstate->env->mesh, qstate->mesh_info, module_event_pass, 905 NULL); 906 } 907 908 /** Setup timer to continue validation of msg signatures later */ 909 static int 910 validate_suspend_setup_timer(struct module_qstate* qstate, 911 struct val_qstate* vq, int id, enum val_state resume_state) 912 { 913 struct timeval tv; 914 int usec, slack, base; 915 if(vq->suspend_count >= MAX_VALIDATION_SUSPENDS) { 916 verbose(VERB_ALGO, "validate_suspend timer: " 917 "reached MAX_VALIDATION_SUSPENDS (%d); error out", 918 MAX_VALIDATION_SUSPENDS); 919 errinf(qstate, "max validation suspends reached, " 920 "too many RRSIG validations"); 921 return 0; 922 } 923 verbose(VERB_ALGO, "validate_suspend timer, set for suspend"); 924 vq->state = resume_state; 925 qstate->ext_state[id] = module_wait_reply; 926 if(!vq->suspend_timer) { 927 vq->suspend_timer = comm_timer_create( 928 qstate->env->worker_base, 929 validate_suspend_timer_cb, qstate); 930 if(!vq->suspend_timer) { 931 log_err("validate_suspend_setup_timer: " 932 "out of memory for comm_timer_create"); 933 return 0; 934 } 935 } 936 /* The timer is activated later, after other events in the event 937 * loop have been processed. The query state can also be deleted, 938 * when the list is full and query states are dropped. */ 939 /* Extend wait time if there are a lot of queries or if this one 940 * is taking long, to keep around cpu time for ordinary queries. */ 941 usec = 50000; /* 50 msec */ 942 slack = 0; 943 if(qstate->env->mesh->all.count >= qstate->env->mesh->max_reply_states) 944 slack += 3; 945 else if(qstate->env->mesh->all.count >= qstate->env->mesh->max_reply_states/2) 946 slack += 2; 947 else if(qstate->env->mesh->all.count >= qstate->env->mesh->max_reply_states/4) 948 slack += 1; 949 /* One step of back-off after the first suspend so a single bad 950 * message still yields, but does not grow exponentially on its own. */ 951 if(vq->suspend_count > 0) 952 slack += 1; 953 if(slack != 0 && slack <= 12 /* No numeric overflow. */) { 954 usec = usec << slack; 955 } 956 /* Spread such timeouts within 90%-100% of the original timer. */ 957 base = usec * 9/10; 958 usec = base + ub_random_max(qstate->env->rnd, usec-base); 959 tv.tv_usec = (usec % 1000000); 960 tv.tv_sec = (usec / 1000000); 961 vq->suspend_count ++; 962 comm_timer_set(vq->suspend_timer, &tv); 963 return 1; 964 } 965 966 /** 967 * Detect wrong truncated response (say from BIND 9.6.1 that is forwarding 968 * and saw the NS record without signatures from a referral). 969 * The positive response has a mangled authority section. 970 * Remove that authority section and the additional section. 971 * @param rep: reply 972 * @return true if a wrongly truncated response. 973 */ 974 static int 975 detect_wrongly_truncated(struct reply_info* rep) 976 { 977 size_t i; 978 /* only NS in authority, and it is bogus */ 979 if(rep->ns_numrrsets != 1 || rep->an_numrrsets == 0) 980 return 0; 981 if(ntohs(rep->rrsets[ rep->an_numrrsets ]->rk.type) != LDNS_RR_TYPE_NS) 982 return 0; 983 if(((struct packed_rrset_data*)rep->rrsets[ rep->an_numrrsets ] 984 ->entry.data)->security == sec_status_secure) 985 return 0; 986 /* answer section is present and secure */ 987 for(i=0; i<rep->an_numrrsets; i++) { 988 if(((struct packed_rrset_data*)rep->rrsets[ i ] 989 ->entry.data)->security != sec_status_secure) 990 return 0; 991 } 992 verbose(VERB_ALGO, "truncating to minimal response"); 993 return 1; 994 } 995 996 /** 997 * For messages that are not referrals, if the chase reply contains an 998 * unsigned NS record in the authority section it could have been 999 * inserted by a (BIND) forwarder that thinks the zone is insecure, and 1000 * that has an NS record without signatures in cache. Remove the NS 1001 * record since the reply does not hinge on that record (in the authority 1002 * section), but do not remove it if it removes the last record from the 1003 * answer+authority sections. 1004 * @param chase_reply: the chased reply, we have a key for this contents, 1005 * so we should have signatures for these rrsets and not having 1006 * signatures means it will be bogus. 1007 * @param orig_reply: original reply, remove NS from there as well because 1008 * we cannot mark the NS record as DNSSEC valid because it is not 1009 * validated by signatures. 1010 */ 1011 static void 1012 remove_spurious_authority(struct reply_info* chase_reply, 1013 struct reply_info* orig_reply) 1014 { 1015 size_t i, found = 0; 1016 int remove = 0; 1017 /* if no answer and only 1 auth RRset, do not remove that one */ 1018 if(chase_reply->an_numrrsets == 0 && chase_reply->ns_numrrsets == 1) 1019 return; 1020 /* search authority section for unsigned NS records */ 1021 for(i = chase_reply->an_numrrsets; 1022 i < chase_reply->an_numrrsets+chase_reply->ns_numrrsets; i++) { 1023 struct packed_rrset_data* d = (struct packed_rrset_data*) 1024 chase_reply->rrsets[i]->entry.data; 1025 if(ntohs(chase_reply->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS 1026 && d->rrsig_count == 0) { 1027 found = i; 1028 remove = 1; 1029 break; 1030 } 1031 } 1032 /* see if we found the entry */ 1033 if(!remove) return; 1034 log_rrset_key(VERB_ALGO, "Removing spurious unsigned NS record " 1035 "(likely inserted by forwarder)", chase_reply->rrsets[found]); 1036 1037 /* find rrset in orig_reply */ 1038 for(i = orig_reply->an_numrrsets; 1039 i < orig_reply->an_numrrsets+orig_reply->ns_numrrsets; i++) { 1040 if(ntohs(orig_reply->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS 1041 && query_dname_compare(orig_reply->rrsets[i]->rk.dname, 1042 chase_reply->rrsets[found]->rk.dname) == 0) { 1043 /* remove from orig_msg */ 1044 val_reply_remove_auth(orig_reply, i); 1045 break; 1046 } 1047 } 1048 /* remove rrset from chase_reply */ 1049 val_reply_remove_auth(chase_reply, found); 1050 } 1051 1052 /** 1053 * Cap the number of answer RRsets for validation of type ANY. 1054 * This limits the number of RRSIG validations performed. 1055 * It is allowed to return a subset of available RRsets when processing 1056 * ANY query. 1057 * @param chase_reply: the chased reply, shorten if if too long. 1058 * @param orig_reply: original reply, remove the records here as well, 1059 * so it can be marked as DNSSEC valid. 1060 * @param skip: the number of rrsets skipped in the answer section due to 1061 * CNAME chain that is followed. 1062 * @param max_rrsets: the number allowed. 1063 */ 1064 static void 1065 shorten_answer_any(struct reply_info* chase_reply, 1066 struct reply_info* orig_reply, size_t skip, size_t max_rrsets) 1067 { 1068 if(chase_reply->an_numrrsets > max_rrsets) { 1069 size_t to_rem = chase_reply->an_numrrsets - max_rrsets; 1070 val_reply_remove_answers(chase_reply, max_rrsets, to_rem); 1071 val_reply_remove_answers(orig_reply, skip+max_rrsets, to_rem); 1072 } 1073 } 1074 1075 /** 1076 * Given a "positive" response -- a response that contains an answer to the 1077 * question, and no CNAME chain, validate this response. 1078 * 1079 * The answer and authority RRsets must already be verified as secure. 1080 * 1081 * @param env: module env for verify. 1082 * @param ve: validator env for verify. 1083 * @param qchase: query that was made. 1084 * @param chase_reply: answer to that query to validate. 1085 * @param kkey: the key entry, which is trusted, and which matches 1086 * the signer of the answer. The key entry isgood(). 1087 * @param qstate: query state for the region. 1088 * @param vq: validator state for the nsec3 cache table. 1089 * @param nsec3_calculations: current nsec3 hash calculations. 1090 * @param suspend: returned true if the task takes too long and needs to 1091 * suspend to continue the effort later. 1092 */ 1093 static void 1094 validate_positive_response(struct module_env* env, struct val_env* ve, 1095 struct query_info* qchase, struct reply_info* chase_reply, 1096 struct key_entry_key* kkey, struct module_qstate* qstate, 1097 struct val_qstate* vq, int* nsec3_calculations, int* suspend) 1098 { 1099 uint8_t* wc = NULL; 1100 size_t wl; 1101 int wc_cached = 0; 1102 int wc_to_cache = 0; 1103 uint8_t* cache_wc = NULL; 1104 size_t cache_wl = 0; 1105 struct ub_packed_rrset_key* cache_s = NULL; 1106 int wc_NSEC_ok = 0; 1107 /* This is used to update the RRset cache, with the combination 1108 * of the dname expansion and this wildcard, for security status. */ 1109 struct ub_packed_rrset_key* wc_rrset = NULL; 1110 int nsec3s_seen = 0; 1111 size_t i; 1112 struct ub_packed_rrset_key* s; 1113 *suspend = 0; 1114 1115 /* validate the ANSWER section - this will be the answer itself */ 1116 for(i=0; i<chase_reply->an_numrrsets; i++) { 1117 s = chase_reply->rrsets[i]; 1118 1119 /* Check to see if the rrset is the result of a wildcard 1120 * expansion. If so, an additional check will need to be 1121 * made in the authority section. */ 1122 if(!val_rrset_wildcard(s, &wc, &wl)) { 1123 log_nametypeclass(VERB_QUERY, "Positive response has " 1124 "inconsistent wildcard sigs:", s->rk.dname, 1125 ntohs(s->rk.type), ntohs(s->rk.rrset_class)); 1126 chase_reply->security = sec_status_bogus; 1127 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1128 if(wc_rrset) 1129 ((struct packed_rrset_data*)wc_rrset-> 1130 entry.data)->security = sec_status_bogus; 1131 return; 1132 } 1133 if(wc && !wc_cached && env->cfg->aggressive_nsec) { 1134 /* Postpone cache adjust until proof has succeeded. */ 1135 wc_to_cache = 1; 1136 cache_wc = wc; 1137 cache_wl = wl; 1138 cache_s = s; 1139 wc_cached = 1; 1140 } 1141 if(wc) wc_rrset = s; 1142 } 1143 1144 /* validate the AUTHORITY section as well - this will generally be 1145 * the NS rrset (which could be missing, no problem) */ 1146 for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+ 1147 chase_reply->ns_numrrsets; i++) { 1148 s = chase_reply->rrsets[i]; 1149 1150 /* If this is a positive wildcard response, and we have a 1151 * (just verified) NSEC record, try to use it to 1) prove 1152 * that qname doesn't exist and 2) that the correct wildcard 1153 * was used. */ 1154 if(wc != NULL && ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) { 1155 if(val_nsec_proves_positive_wildcard(s, qchase, wc)) { 1156 wc_NSEC_ok = 1; 1157 } 1158 /* if not, continue looking for proof */ 1159 } 1160 1161 /* Otherwise, if this is a positive wildcard response and 1162 * we have NSEC3 records */ 1163 if(wc != NULL && ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) { 1164 nsec3s_seen = 1; 1165 } 1166 } 1167 1168 /* If this was a positive wildcard response that we haven't already 1169 * proven, and we have NSEC3 records, try to prove it using the NSEC3 1170 * records. */ 1171 if(wc != NULL && !wc_NSEC_ok && nsec3s_seen && 1172 nsec3_cache_table_init(&vq->nsec3_cache_table, qstate->region)) { 1173 enum sec_status sec = nsec3_prove_wildcard(env, ve, 1174 chase_reply->rrsets+chase_reply->an_numrrsets, 1175 chase_reply->ns_numrrsets, qchase, kkey, wc, 1176 &vq->nsec3_cache_table, nsec3_calculations); 1177 if(sec == sec_status_insecure) { 1178 verbose(VERB_ALGO, "Positive wildcard response is " 1179 "insecure"); 1180 chase_reply->security = sec_status_insecure; 1181 return; 1182 } else if(sec == sec_status_secure) { 1183 wc_NSEC_ok = 1; 1184 } else if(sec == sec_status_unchecked) { 1185 *suspend = 1; 1186 return; 1187 } 1188 } 1189 1190 /* If after all this, we still haven't proven the positive wildcard 1191 * response, fail. */ 1192 if(wc != NULL && !wc_NSEC_ok) { 1193 verbose(VERB_QUERY, "positive response was wildcard " 1194 "expansion and did not prove original data " 1195 "did not exist"); 1196 chase_reply->security = sec_status_bogus; 1197 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1198 if(wc_rrset) 1199 ((struct packed_rrset_data*)wc_rrset-> 1200 entry.data)->security = sec_status_bogus; 1201 return; 1202 } 1203 if(wc_to_cache) { 1204 rrset_cache_update_wildcard(env->rrset_cache, cache_s, 1205 cache_wc, cache_wl, env->alloc, *env->now); 1206 } 1207 1208 verbose(VERB_ALGO, "Successfully validated positive response"); 1209 chase_reply->security = sec_status_secure; 1210 } 1211 1212 /** 1213 * Validate a NOERROR/NODATA signed response -- a response that has a 1214 * NOERROR Rcode but no ANSWER section RRsets. This consists of making 1215 * certain that the authority section NSEC/NSEC3s proves that the qname 1216 * does exist and the qtype doesn't. 1217 * 1218 * The answer and authority RRsets must already be verified as secure. 1219 * 1220 * @param env: module env for verify. 1221 * @param ve: validator env for verify. 1222 * @param qchase: query that was made. 1223 * @param chase_reply: answer to that query to validate. 1224 * @param kkey: the key entry, which is trusted, and which matches 1225 * the signer of the answer. The key entry isgood(). 1226 * @param qstate: query state for the region. 1227 * @param vq: validator state for the nsec3 cache table. 1228 * @param nsec3_calculations: current nsec3 hash calculations. 1229 * @param suspend: returned true if the task takes too long and needs to 1230 * suspend to continue the effort later. 1231 */ 1232 static void 1233 validate_nodata_response(struct module_env* env, struct val_env* ve, 1234 struct query_info* qchase, struct reply_info* chase_reply, 1235 struct key_entry_key* kkey, struct module_qstate* qstate, 1236 struct val_qstate* vq, int* nsec3_calculations, int* suspend) 1237 { 1238 /* Since we are here, there must be nothing in the ANSWER section to 1239 * validate. */ 1240 /* (Note: CNAME/DNAME responses will not directly get here -- 1241 * instead, they are chased down into individual CNAME validations, 1242 * and at the end of the cname chain a POSITIVE, or CNAME_NOANSWER 1243 * validation.) */ 1244 1245 /* validate the AUTHORITY section */ 1246 int has_valid_nsec = 0; /* If true, then the NODATA has been proven.*/ 1247 uint8_t* ce = NULL; /* for wildcard nodata responses. This is the 1248 proven closest encloser. */ 1249 uint8_t* wc = NULL; /* for wildcard nodata responses. wildcard nsec */ 1250 int nsec3s_seen = 0; /* nsec3s seen */ 1251 struct ub_packed_rrset_key* s; 1252 size_t i; 1253 *suspend = 0; 1254 1255 for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+ 1256 chase_reply->ns_numrrsets; i++) { 1257 s = chase_reply->rrsets[i]; 1258 /* If we encounter an NSEC record, try to use it to prove 1259 * NODATA. 1260 * This needs to handle the ENT NODATA case. */ 1261 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) { 1262 if(nsec_proves_nodata(s, qchase, &wc)) { 1263 has_valid_nsec = 1; 1264 /* sets wc-encloser if wildcard applicable */ 1265 } 1266 if(val_nsec_proves_name_error(s, qchase->qname)) { 1267 ce = nsec_closest_encloser(qchase->qname, s); 1268 } 1269 if(val_nsec_proves_insecuredelegation(s, qchase)) { 1270 verbose(VERB_ALGO, "delegation is insecure"); 1271 chase_reply->security = sec_status_insecure; 1272 return; 1273 } 1274 } else if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) { 1275 nsec3s_seen = 1; 1276 } 1277 } 1278 1279 /* check to see if we have a wildcard NODATA proof. */ 1280 1281 /* The wildcard NODATA is 1 NSEC proving that qname does not exist 1282 * (and also proving what the closest encloser is), and 1 NSEC 1283 * showing the matching wildcard, which must be *.closest_encloser. */ 1284 if(wc && !ce) 1285 has_valid_nsec = 0; 1286 else if(wc && ce) { 1287 if(query_dname_compare(wc, ce) != 0) { 1288 has_valid_nsec = 0; 1289 } 1290 } 1291 1292 if(!has_valid_nsec && nsec3s_seen && 1293 nsec3_cache_table_init(&vq->nsec3_cache_table, qstate->region)) { 1294 enum sec_status sec = nsec3_prove_nodata(env, ve, 1295 chase_reply->rrsets+chase_reply->an_numrrsets, 1296 chase_reply->ns_numrrsets, qchase, kkey, 1297 &vq->nsec3_cache_table, nsec3_calculations); 1298 if(sec == sec_status_insecure) { 1299 verbose(VERB_ALGO, "NODATA response is insecure"); 1300 chase_reply->security = sec_status_insecure; 1301 return; 1302 } else if(sec == sec_status_secure) { 1303 has_valid_nsec = 1; 1304 } else if(sec == sec_status_unchecked) { 1305 /* check is incomplete; suspend */ 1306 *suspend = 1; 1307 return; 1308 } 1309 } 1310 1311 if(!has_valid_nsec) { 1312 verbose(VERB_QUERY, "NODATA response failed to prove NODATA " 1313 "status with NSEC/NSEC3"); 1314 if(verbosity >= VERB_ALGO) 1315 log_dns_msg("Failed NODATA", qchase, chase_reply); 1316 chase_reply->security = sec_status_bogus; 1317 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1318 return; 1319 } 1320 1321 verbose(VERB_ALGO, "successfully validated NODATA response."); 1322 chase_reply->security = sec_status_secure; 1323 } 1324 1325 /** 1326 * Validate a NAMEERROR signed response -- a response that has a NXDOMAIN 1327 * Rcode. 1328 * This consists of making certain that the authority section NSEC proves 1329 * that the qname doesn't exist and the covering wildcard also doesn't exist.. 1330 * 1331 * The answer and authority RRsets must have already been verified as secure. 1332 * 1333 * @param env: module env for verify. 1334 * @param ve: validator env for verify. 1335 * @param qchase: query that was made. 1336 * @param chase_reply: answer to that query to validate. 1337 * @param kkey: the key entry, which is trusted, and which matches 1338 * the signer of the answer. The key entry isgood(). 1339 * @param rcode: adjusted RCODE, in case of RCODE/proof mismatch leniency. 1340 * @param qstate: query state for the region. 1341 * @param vq: validator state for the nsec3 cache table. 1342 * @param nsec3_calculations: current nsec3 hash calculations. 1343 * @param suspend: returned true if the task takes too long and needs to 1344 * suspend to continue the effort later. 1345 */ 1346 static void 1347 validate_nameerror_response(struct module_env* env, struct val_env* ve, 1348 struct query_info* qchase, struct reply_info* chase_reply, 1349 struct key_entry_key* kkey, int* rcode, 1350 struct module_qstate* qstate, struct val_qstate* vq, 1351 int* nsec3_calculations, int* suspend) 1352 { 1353 int has_valid_nsec = 0; 1354 int has_valid_wnsec = 0; 1355 int nsec3s_seen = 0; 1356 struct ub_packed_rrset_key* s; 1357 size_t i; 1358 uint8_t* ce; 1359 int ce_labs = 0; 1360 int prev_ce_labs = 0; 1361 *suspend = 0; 1362 1363 for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+ 1364 chase_reply->ns_numrrsets; i++) { 1365 s = chase_reply->rrsets[i]; 1366 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) { 1367 if(val_nsec_proves_name_error(s, qchase->qname)) 1368 has_valid_nsec = 1; 1369 ce = nsec_closest_encloser(qchase->qname, s); 1370 ce_labs = dname_count_labels(ce); 1371 /* Use longest closest encloser to prove wildcard. */ 1372 if(ce_labs > prev_ce_labs || 1373 (ce_labs == prev_ce_labs && 1374 has_valid_wnsec == 0)) { 1375 if(val_nsec_proves_no_wc(s, qchase->qname, 1376 qchase->qname_len)) 1377 has_valid_wnsec = 1; 1378 else 1379 has_valid_wnsec = 0; 1380 } 1381 prev_ce_labs = ce_labs; 1382 if(val_nsec_proves_insecuredelegation(s, qchase)) { 1383 verbose(VERB_ALGO, "delegation is insecure"); 1384 chase_reply->security = sec_status_insecure; 1385 return; 1386 } 1387 } else if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) 1388 nsec3s_seen = 1; 1389 } 1390 1391 if((!has_valid_nsec || !has_valid_wnsec) && nsec3s_seen && 1392 nsec3_cache_table_init(&vq->nsec3_cache_table, qstate->region)) { 1393 /* use NSEC3 proof, both answer and auth rrsets, in case 1394 * NSEC3s end up in the answer (due to qtype=NSEC3 or so) */ 1395 chase_reply->security = nsec3_prove_nameerror(env, ve, 1396 chase_reply->rrsets, chase_reply->an_numrrsets+ 1397 chase_reply->ns_numrrsets, qchase, kkey, 1398 &vq->nsec3_cache_table, nsec3_calculations); 1399 if(chase_reply->security == sec_status_unchecked) { 1400 *suspend = 1; 1401 return; 1402 } else if(chase_reply->security != sec_status_secure) { 1403 verbose(VERB_QUERY, "NameError response failed nsec, " 1404 "nsec3 proof was %s", sec_status_to_string( 1405 chase_reply->security)); 1406 return; 1407 } 1408 has_valid_nsec = 1; 1409 has_valid_wnsec = 1; 1410 } 1411 1412 /* If the message fails to prove either condition, it is bogus. */ 1413 if(!has_valid_nsec) { 1414 validate_nodata_response(env, ve, qchase, chase_reply, kkey, 1415 qstate, vq, nsec3_calculations, suspend); 1416 if(*suspend) return; 1417 verbose(VERB_QUERY, "NameError response has failed to prove: " 1418 "qname does not exist"); 1419 /* Be lenient with RCODE in NSEC NameError responses */ 1420 if(chase_reply->security == sec_status_secure) { 1421 *rcode = LDNS_RCODE_NOERROR; 1422 } else { 1423 chase_reply->security = sec_status_bogus; 1424 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1425 } 1426 return; 1427 } 1428 1429 if(!has_valid_wnsec) { 1430 validate_nodata_response(env, ve, qchase, chase_reply, kkey, 1431 qstate, vq, nsec3_calculations, suspend); 1432 if(*suspend) return; 1433 verbose(VERB_QUERY, "NameError response has failed to prove: " 1434 "covering wildcard does not exist"); 1435 /* Be lenient with RCODE in NSEC NameError responses */ 1436 if (chase_reply->security == sec_status_secure) { 1437 *rcode = LDNS_RCODE_NOERROR; 1438 } else { 1439 chase_reply->security = sec_status_bogus; 1440 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1441 } 1442 return; 1443 } 1444 1445 /* Otherwise, we consider the message secure. */ 1446 verbose(VERB_ALGO, "successfully validated NAME ERROR response."); 1447 chase_reply->security = sec_status_secure; 1448 } 1449 1450 /** 1451 * Given a referral response, validate rrsets and take least trusted rrset 1452 * as the current validation status. 1453 * 1454 * Note that by the time this method is called, the process of finding the 1455 * trusted DNSKEY rrset that signs this response must already have been 1456 * completed. 1457 * 1458 * @param env: module env. 1459 * @param chase_reply: answer to validate. 1460 */ 1461 static void 1462 validate_referral_response(struct module_env* env, struct reply_info* chase_reply) 1463 { 1464 size_t i, count; 1465 enum sec_status s; 1466 /* message security equals lowest rrset security */ 1467 chase_reply->security = sec_status_secure; 1468 if(env->cfg->val_clean_additional) 1469 count = chase_reply->rrset_count; 1470 else count = chase_reply->an_numrrsets+chase_reply->ns_numrrsets; 1471 for(i=0; i<count; i++) { 1472 s = ((struct packed_rrset_data*)chase_reply->rrsets[i] 1473 ->entry.data)->security; 1474 if(s < chase_reply->security) 1475 chase_reply->security = s; 1476 } 1477 verbose(VERB_ALGO, "validated part of referral response as %s", 1478 sec_status_to_string(chase_reply->security)); 1479 } 1480 1481 /** 1482 * Given an "ANY" response -- a response that contains an answer to a 1483 * qtype==ANY question, with answers. This does no checking that all 1484 * types are present. 1485 * 1486 * NOTE: it may be possible to get parent-side delegation point records 1487 * here, which won't all be signed. Right now, this routine relies on the 1488 * upstream iterative resolver to not return these responses -- instead 1489 * treating them as referrals. 1490 * 1491 * NOTE: RFC 4035 is silent on this issue, so this may change upon 1492 * clarification. Clarification draft -05 says to not check all types are 1493 * present. 1494 * 1495 * Note that by the time this method is called, the process of finding the 1496 * trusted DNSKEY rrset that signs this response must already have been 1497 * completed. 1498 * 1499 * @param env: module env for verify. 1500 * @param ve: validator env for verify. 1501 * @param qchase: query that was made. 1502 * @param chase_reply: answer to that query to validate. 1503 * @param kkey: the key entry, which is trusted, and which matches 1504 * the signer of the answer. The key entry isgood(). 1505 * @param qstate: query state for the region. 1506 * @param vq: validator state for the nsec3 cache table. 1507 * @param nsec3_calculations: current nsec3 hash calculations. 1508 * @param suspend: returned true if the task takes too long and needs to 1509 * suspend to continue the effort later. 1510 */ 1511 static void 1512 validate_any_response(struct module_env* env, struct val_env* ve, 1513 struct query_info* qchase, struct reply_info* chase_reply, 1514 struct key_entry_key* kkey, struct module_qstate* qstate, 1515 struct val_qstate* vq, int* nsec3_calculations, int* suspend) 1516 { 1517 /* all answer and auth rrsets already verified */ 1518 /* but check if a wildcard response is given, then check NSEC/NSEC3 1519 * for qname denial to see if wildcard is applicable */ 1520 uint8_t* wc = NULL; 1521 size_t wl; 1522 int wc_NSEC_ok = 0; 1523 int nsec3s_seen = 0; 1524 size_t i; 1525 struct ub_packed_rrset_key* s; 1526 *suspend = 0; 1527 1528 if(qchase->qtype != LDNS_RR_TYPE_ANY) { 1529 log_err("internal error: ANY validation called for non-ANY"); 1530 chase_reply->security = sec_status_bogus; 1531 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1532 return; 1533 } 1534 1535 /* validate the ANSWER section - this will be the answer itself */ 1536 for(i=0; i<chase_reply->an_numrrsets; i++) { 1537 s = chase_reply->rrsets[i]; 1538 1539 /* Check to see if the rrset is the result of a wildcard 1540 * expansion. If so, an additional check will need to be 1541 * made in the authority section. */ 1542 if(!val_rrset_wildcard(s, &wc, &wl)) { 1543 log_nametypeclass(VERB_QUERY, "Positive ANY response" 1544 " has inconsistent wildcard sigs:", 1545 s->rk.dname, ntohs(s->rk.type), 1546 ntohs(s->rk.rrset_class)); 1547 chase_reply->security = sec_status_bogus; 1548 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1549 return; 1550 } 1551 } 1552 1553 /* if it was a wildcard, check for NSEC/NSEC3s in both answer 1554 * and authority sections (NSEC may be moved to the ANSWER section) */ 1555 if(wc != NULL) 1556 for(i=0; i<chase_reply->an_numrrsets+chase_reply->ns_numrrsets; 1557 i++) { 1558 s = chase_reply->rrsets[i]; 1559 1560 /* If this is a positive wildcard response, and we have a 1561 * (just verified) NSEC record, try to use it to 1) prove 1562 * that qname doesn't exist and 2) that the correct wildcard 1563 * was used. */ 1564 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) { 1565 if(val_nsec_proves_positive_wildcard(s, qchase, wc)) { 1566 wc_NSEC_ok = 1; 1567 } 1568 /* if not, continue looking for proof */ 1569 } 1570 1571 /* Otherwise, if this is a positive wildcard response and 1572 * we have NSEC3 records */ 1573 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) { 1574 nsec3s_seen = 1; 1575 } 1576 } 1577 1578 /* If this was a positive wildcard response that we haven't already 1579 * proven, and we have NSEC3 records, try to prove it using the NSEC3 1580 * records. */ 1581 if(wc != NULL && !wc_NSEC_ok && nsec3s_seen && 1582 nsec3_cache_table_init(&vq->nsec3_cache_table, qstate->region)) { 1583 /* look both in answer and auth section for NSEC3s */ 1584 enum sec_status sec = nsec3_prove_wildcard(env, ve, 1585 chase_reply->rrsets, 1586 chase_reply->an_numrrsets+chase_reply->ns_numrrsets, 1587 qchase, kkey, wc, &vq->nsec3_cache_table, 1588 nsec3_calculations); 1589 if(sec == sec_status_insecure) { 1590 verbose(VERB_ALGO, "Positive ANY wildcard response is " 1591 "insecure"); 1592 chase_reply->security = sec_status_insecure; 1593 return; 1594 } else if(sec == sec_status_secure) { 1595 wc_NSEC_ok = 1; 1596 } else if(sec == sec_status_unchecked) { 1597 *suspend = 1; 1598 return; 1599 } 1600 } 1601 1602 /* If after all this, we still haven't proven the positive wildcard 1603 * response, fail. */ 1604 if(wc != NULL && !wc_NSEC_ok) { 1605 verbose(VERB_QUERY, "positive ANY response was wildcard " 1606 "expansion and did not prove original data " 1607 "did not exist"); 1608 chase_reply->security = sec_status_bogus; 1609 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1610 /* Make the expanded name and wildcard RRSIG rrsets bogus */ 1611 for(i=0; i<chase_reply->an_numrrsets; i++) { 1612 uint8_t* cwc = NULL; 1613 size_t cwl = 0; 1614 s = chase_reply->rrsets[i]; 1615 if(val_rrset_wildcard(s, &cwc, &cwl) && cwc) { 1616 ((struct packed_rrset_data*)s-> 1617 entry.data)->security = sec_status_bogus; 1618 } 1619 } 1620 return; 1621 } 1622 1623 verbose(VERB_ALGO, "Successfully validated positive ANY response"); 1624 chase_reply->security = sec_status_secure; 1625 } 1626 1627 /** 1628 * Validate CNAME response, or DNAME+CNAME. 1629 * This is just like a positive proof, except that this is about a 1630 * DNAME+CNAME. Possible wildcard proof. 1631 * Difference with positive proof is that this routine refuses 1632 * wildcarded DNAMEs. 1633 * 1634 * The answer and authority rrsets must already be verified as secure. 1635 * 1636 * @param env: module env for verify. 1637 * @param ve: validator env for verify. 1638 * @param qchase: query that was made. 1639 * @param chase_reply: answer to that query to validate. 1640 * @param kkey: the key entry, which is trusted, and which matches 1641 * the signer of the answer. The key entry isgood(). 1642 * @param qstate: query state for the region. 1643 * @param vq: validator state for the nsec3 cache table. 1644 * @param nsec3_calculations: current nsec3 hash calculations. 1645 * @param suspend: returned true if the task takes too long and needs to 1646 * suspend to continue the effort later. 1647 */ 1648 static void 1649 validate_cname_response(struct module_env* env, struct val_env* ve, 1650 struct query_info* qchase, struct reply_info* chase_reply, 1651 struct key_entry_key* kkey, struct module_qstate* qstate, 1652 struct val_qstate* vq, int* nsec3_calculations, int* suspend) 1653 { 1654 uint8_t* wc = NULL; 1655 size_t wl; 1656 int wc_NSEC_ok = 0; 1657 /* This is used to update the RRset cache, with the combination 1658 * of the dname expansion and this wildcard, for security status. */ 1659 struct ub_packed_rrset_key* wc_rrset = NULL; 1660 int nsec3s_seen = 0; 1661 size_t i; 1662 struct ub_packed_rrset_key* s; 1663 *suspend = 0; 1664 1665 /* validate the ANSWER section - this will be the CNAME (+DNAME) */ 1666 for(i=0; i<chase_reply->an_numrrsets; i++) { 1667 s = chase_reply->rrsets[i]; 1668 1669 /* Check to see if the rrset is the result of a wildcard 1670 * expansion. If so, an additional check will need to be 1671 * made in the authority section. */ 1672 if(!val_rrset_wildcard(s, &wc, &wl)) { 1673 log_nametypeclass(VERB_QUERY, "Cname response has " 1674 "inconsistent wildcard sigs:", s->rk.dname, 1675 ntohs(s->rk.type), ntohs(s->rk.rrset_class)); 1676 chase_reply->security = sec_status_bogus; 1677 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1678 return; 1679 } 1680 if(wc) wc_rrset = s; 1681 1682 /* Refuse wildcarded DNAMEs rfc 4597. 1683 * Do not follow a wildcarded DNAME because 1684 * its synthesized CNAME expansion is underdefined */ 1685 if(qchase->qtype != LDNS_RR_TYPE_DNAME && 1686 ntohs(s->rk.type) == LDNS_RR_TYPE_DNAME && wc) { 1687 log_nametypeclass(VERB_QUERY, "cannot validate a " 1688 "wildcarded DNAME:", s->rk.dname, 1689 ntohs(s->rk.type), ntohs(s->rk.rrset_class)); 1690 chase_reply->security = sec_status_bogus; 1691 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1692 if(wc_rrset) 1693 ((struct packed_rrset_data*)wc_rrset-> 1694 entry.data)->security = sec_status_bogus; 1695 return; 1696 } 1697 1698 /* If we have found a CNAME, stop looking for one. 1699 * The iterator has placed the CNAME chain in correct 1700 * order. */ 1701 if (ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME) { 1702 break; 1703 } 1704 } 1705 1706 /* AUTHORITY section */ 1707 for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+ 1708 chase_reply->ns_numrrsets; i++) { 1709 s = chase_reply->rrsets[i]; 1710 1711 /* If this is a positive wildcard response, and we have a 1712 * (just verified) NSEC record, try to use it to 1) prove 1713 * that qname doesn't exist and 2) that the correct wildcard 1714 * was used. */ 1715 if(wc != NULL && ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) { 1716 if(val_nsec_proves_positive_wildcard(s, qchase, wc)) { 1717 wc_NSEC_ok = 1; 1718 } 1719 /* if not, continue looking for proof */ 1720 } 1721 1722 /* Otherwise, if this is a positive wildcard response and 1723 * we have NSEC3 records */ 1724 if(wc != NULL && ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) { 1725 nsec3s_seen = 1; 1726 } 1727 } 1728 1729 /* If this was a positive wildcard response that we haven't already 1730 * proven, and we have NSEC3 records, try to prove it using the NSEC3 1731 * records. */ 1732 if(wc != NULL && !wc_NSEC_ok && nsec3s_seen && 1733 nsec3_cache_table_init(&vq->nsec3_cache_table, qstate->region)) { 1734 enum sec_status sec = nsec3_prove_wildcard(env, ve, 1735 chase_reply->rrsets+chase_reply->an_numrrsets, 1736 chase_reply->ns_numrrsets, qchase, kkey, wc, 1737 &vq->nsec3_cache_table, nsec3_calculations); 1738 if(sec == sec_status_insecure) { 1739 verbose(VERB_ALGO, "wildcard CNAME response is " 1740 "insecure"); 1741 chase_reply->security = sec_status_insecure; 1742 return; 1743 } else if(sec == sec_status_secure) { 1744 wc_NSEC_ok = 1; 1745 } else if(sec == sec_status_unchecked) { 1746 *suspend = 1; 1747 return; 1748 } 1749 } 1750 1751 /* If after all this, we still haven't proven the positive wildcard 1752 * response, fail. */ 1753 if(wc != NULL && !wc_NSEC_ok) { 1754 verbose(VERB_QUERY, "CNAME response was wildcard " 1755 "expansion and did not prove original data " 1756 "did not exist"); 1757 chase_reply->security = sec_status_bogus; 1758 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1759 if(wc_rrset) 1760 ((struct packed_rrset_data*)wc_rrset-> 1761 entry.data)->security = sec_status_bogus; 1762 return; 1763 } 1764 1765 verbose(VERB_ALGO, "Successfully validated CNAME response"); 1766 chase_reply->security = sec_status_secure; 1767 } 1768 1769 /** 1770 * Validate CNAME NOANSWER response, no more data after a CNAME chain. 1771 * This can be a NODATA or a NAME ERROR case, but not both at the same time. 1772 * We don't know because the rcode has been set to NOERROR by the CNAME. 1773 * 1774 * The answer and authority rrsets must already be verified as secure. 1775 * 1776 * @param env: module env for verify. 1777 * @param ve: validator env for verify. 1778 * @param qchase: query that was made. 1779 * @param chase_reply: answer to that query to validate. 1780 * @param kkey: the key entry, which is trusted, and which matches 1781 * the signer of the answer. The key entry isgood(). 1782 * @param qstate: query state for the region. 1783 * @param vq: validator state for the nsec3 cache table. 1784 * @param nsec3_calculations: current nsec3 hash calculations. 1785 * @param suspend: returned true if the task takes too long and needs to 1786 * suspend to continue the effort later. 1787 */ 1788 static void 1789 validate_cname_noanswer_response(struct module_env* env, struct val_env* ve, 1790 struct query_info* qchase, struct reply_info* chase_reply, 1791 struct key_entry_key* kkey, struct module_qstate* qstate, 1792 struct val_qstate* vq, int* nsec3_calculations, int* suspend) 1793 { 1794 int nodata_valid_nsec = 0; /* If true, then NODATA has been proven.*/ 1795 uint8_t* ce = NULL; /* for wildcard nodata responses. This is the 1796 proven closest encloser. */ 1797 uint8_t* wc = NULL; /* for wildcard nodata responses. wildcard nsec */ 1798 int nxdomain_valid_nsec = 0; /* if true, nameerror has been proven */ 1799 int nxdomain_valid_wnsec = 0; 1800 int nsec3s_seen = 0; /* nsec3s seen */ 1801 struct ub_packed_rrset_key* s; 1802 size_t i; 1803 uint8_t* nsec_ce; /* Used to find the NSEC with the longest ce */ 1804 int ce_labs = 0; 1805 int prev_ce_labs = 0; 1806 *suspend = 0; 1807 1808 /* the AUTHORITY section */ 1809 for(i=chase_reply->an_numrrsets; i<chase_reply->an_numrrsets+ 1810 chase_reply->ns_numrrsets; i++) { 1811 s = chase_reply->rrsets[i]; 1812 1813 /* If we encounter an NSEC record, try to use it to prove 1814 * NODATA. This needs to handle the ENT NODATA case. 1815 * Also try to prove NAMEERROR, and absence of a wildcard */ 1816 if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC) { 1817 if(nsec_proves_nodata(s, qchase, &wc)) { 1818 nodata_valid_nsec = 1; 1819 /* set wc encloser if wildcard applicable */ 1820 } 1821 if(val_nsec_proves_name_error(s, qchase->qname)) { 1822 ce = nsec_closest_encloser(qchase->qname, s); 1823 nxdomain_valid_nsec = 1; 1824 } 1825 nsec_ce = nsec_closest_encloser(qchase->qname, s); 1826 ce_labs = dname_count_labels(nsec_ce); 1827 /* Use longest closest encloser to prove wildcard. */ 1828 if(ce_labs > prev_ce_labs || 1829 (ce_labs == prev_ce_labs && 1830 nxdomain_valid_wnsec == 0)) { 1831 if(val_nsec_proves_no_wc(s, qchase->qname, 1832 qchase->qname_len)) 1833 nxdomain_valid_wnsec = 1; 1834 else 1835 nxdomain_valid_wnsec = 0; 1836 } 1837 prev_ce_labs = ce_labs; 1838 if(val_nsec_proves_insecuredelegation(s, qchase)) { 1839 verbose(VERB_ALGO, "delegation is insecure"); 1840 chase_reply->security = sec_status_insecure; 1841 return; 1842 } 1843 } else if(ntohs(s->rk.type) == LDNS_RR_TYPE_NSEC3) { 1844 nsec3s_seen = 1; 1845 } 1846 } 1847 1848 /* check to see if we have a wildcard NODATA proof. */ 1849 1850 /* The wildcard NODATA is 1 NSEC proving that qname does not exists 1851 * (and also proving what the closest encloser is), and 1 NSEC 1852 * showing the matching wildcard, which must be *.closest_encloser. */ 1853 if(wc && !ce) 1854 nodata_valid_nsec = 0; 1855 else if(wc && ce) { 1856 if(query_dname_compare(wc, ce) != 0) { 1857 nodata_valid_nsec = 0; 1858 } 1859 } 1860 if(nxdomain_valid_nsec && !nxdomain_valid_wnsec) { 1861 /* name error is missing wildcard denial proof */ 1862 nxdomain_valid_nsec = 0; 1863 } 1864 1865 if(nodata_valid_nsec && nxdomain_valid_nsec) { 1866 verbose(VERB_QUERY, "CNAMEchain to noanswer proves that name " 1867 "exists and not exists, bogus"); 1868 chase_reply->security = sec_status_bogus; 1869 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1870 return; 1871 } 1872 if(!nodata_valid_nsec && !nxdomain_valid_nsec && nsec3s_seen && 1873 nsec3_cache_table_init(&vq->nsec3_cache_table, qstate->region)) { 1874 int nodata; 1875 enum sec_status sec = nsec3_prove_nxornodata(env, ve, 1876 chase_reply->rrsets+chase_reply->an_numrrsets, 1877 chase_reply->ns_numrrsets, qchase, kkey, &nodata, 1878 &vq->nsec3_cache_table, nsec3_calculations); 1879 if(sec == sec_status_insecure) { 1880 verbose(VERB_ALGO, "CNAMEchain to noanswer response " 1881 "is insecure"); 1882 chase_reply->security = sec_status_insecure; 1883 return; 1884 } else if(sec == sec_status_secure) { 1885 if(nodata) 1886 nodata_valid_nsec = 1; 1887 else nxdomain_valid_nsec = 1; 1888 } else if(sec == sec_status_unchecked) { 1889 *suspend = 1; 1890 return; 1891 } 1892 } 1893 1894 if(!nodata_valid_nsec && !nxdomain_valid_nsec) { 1895 verbose(VERB_QUERY, "CNAMEchain to noanswer response failed " 1896 "to prove status with NSEC/NSEC3"); 1897 if(verbosity >= VERB_ALGO) 1898 log_dns_msg("Failed CNAMEnoanswer", qchase, chase_reply); 1899 chase_reply->security = sec_status_bogus; 1900 update_reason_bogus(chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1901 return; 1902 } 1903 1904 if(nodata_valid_nsec) 1905 verbose(VERB_ALGO, "successfully validated CNAME chain to a " 1906 "NODATA response."); 1907 else verbose(VERB_ALGO, "successfully validated CNAME chain to a " 1908 "NAMEERROR response."); 1909 chase_reply->security = sec_status_secure; 1910 } 1911 1912 /** 1913 * Process init state for validator. 1914 * Process the INIT state. First tier responses start in the INIT state. 1915 * This is where they are vetted for validation suitability, and the initial 1916 * key search is done. 1917 * 1918 * Currently, events the come through this routine will be either promoted 1919 * to FINISHED/CNAME_RESP (no validation needed), FINDKEY (next step to 1920 * validation), or will be (temporarily) retired and a new priming request 1921 * event will be generated. 1922 * 1923 * @param qstate: query state. 1924 * @param vq: validator query state. 1925 * @param ve: validator shared global environment. 1926 * @param id: module id. 1927 * @return true if the event should be processed further on return, false if 1928 * not. 1929 */ 1930 static int 1931 processInit(struct module_qstate* qstate, struct val_qstate* vq, 1932 struct val_env* ve, int id) 1933 { 1934 uint8_t* lookup_name; 1935 size_t lookup_len; 1936 struct trust_anchor* anchor; 1937 enum val_classification subtype = val_classify_response( 1938 qstate->query_flags, &qstate->qinfo, &vq->qchase, 1939 vq->orig_msg->rep, vq->rrset_skip); 1940 if(vq->restart_count > ve->max_restart) { 1941 verbose(VERB_ALGO, "restart count exceeded"); 1942 return val_error(qstate, id); 1943 } 1944 1945 /* correctly initialize reason_bogus */ 1946 update_reason_bogus(vq->chase_reply, LDNS_EDE_DNSSEC_BOGUS); 1947 1948 verbose(VERB_ALGO, "validator classification %s", 1949 val_classification_to_string(subtype)); 1950 if(subtype == VAL_CLASS_REFERRAL && 1951 vq->rrset_skip < vq->orig_msg->rep->rrset_count) { 1952 /* referral uses the rrset name as qchase, to find keys for 1953 * that rrset */ 1954 vq->qchase.qname = vq->orig_msg->rep-> 1955 rrsets[vq->rrset_skip]->rk.dname; 1956 vq->qchase.qname_len = vq->orig_msg->rep-> 1957 rrsets[vq->rrset_skip]->rk.dname_len; 1958 vq->qchase.qtype = ntohs(vq->orig_msg->rep-> 1959 rrsets[vq->rrset_skip]->rk.type); 1960 vq->qchase.qclass = ntohs(vq->orig_msg->rep-> 1961 rrsets[vq->rrset_skip]->rk.rrset_class); 1962 } 1963 lookup_name = vq->qchase.qname; 1964 lookup_len = vq->qchase.qname_len; 1965 /* for type DS look at the parent side for keys/trustanchor */ 1966 /* also for NSEC not at apex */ 1967 if(vq->qchase.qtype == LDNS_RR_TYPE_DS || 1968 (vq->qchase.qtype == LDNS_RR_TYPE_NSEC && 1969 vq->orig_msg->rep->rrset_count > vq->rrset_skip && 1970 ntohs(vq->orig_msg->rep->rrsets[vq->rrset_skip]->rk.type) == 1971 LDNS_RR_TYPE_NSEC && 1972 !(vq->orig_msg->rep->rrsets[vq->rrset_skip]-> 1973 rk.flags&PACKED_RRSET_NSEC_AT_APEX))) { 1974 dname_remove_label(&lookup_name, &lookup_len); 1975 } 1976 1977 val_mark_indeterminate(vq->chase_reply, qstate->env->anchors, 1978 qstate->env->rrset_cache, qstate->env); 1979 vq->key_entry = NULL; 1980 vq->empty_DS_name = NULL; 1981 vq->ds_rrset = 0; 1982 anchor = anchors_lookup(qstate->env->anchors, 1983 lookup_name, lookup_len, vq->qchase.qclass); 1984 1985 /* Determine the signer/lookup name */ 1986 val_find_signer(subtype, &vq->qchase, vq->orig_msg->rep, 1987 vq->rrset_skip, &vq->signer_name, &vq->signer_len); 1988 if(vq->signer_name != NULL && 1989 !dname_subdomain_c(lookup_name, vq->signer_name)) { 1990 log_nametypeclass(VERB_ALGO, "this signer name is not a parent " 1991 "of lookupname, omitted", vq->signer_name, 0, 0); 1992 vq->signer_name = NULL; 1993 } 1994 if(vq->signer_name == NULL) { 1995 log_nametypeclass(VERB_ALGO, "no signer, using", lookup_name, 1996 0, 0); 1997 } else { 1998 lookup_name = vq->signer_name; 1999 lookup_len = vq->signer_len; 2000 log_nametypeclass(VERB_ALGO, "signer is", lookup_name, 0, 0); 2001 } 2002 2003 /* for NXDOMAIN it could be signed by a parent of the trust anchor */ 2004 if(subtype == VAL_CLASS_NAMEERROR && vq->signer_name && 2005 anchor && dname_strict_subdomain_c(anchor->name, lookup_name)){ 2006 lock_basic_unlock(&anchor->lock); 2007 anchor = anchors_lookup(qstate->env->anchors, 2008 lookup_name, lookup_len, vq->qchase.qclass); 2009 if(!anchor) { /* unsigned parent denies anchor*/ 2010 verbose(VERB_QUERY, "unsigned parent zone denies" 2011 " trust anchor, indeterminate"); 2012 vq->chase_reply->security = sec_status_indeterminate; 2013 update_reason_bogus(vq->chase_reply, LDNS_EDE_DNSSEC_INDETERMINATE); 2014 vq->state = VAL_FINISHED_STATE; 2015 return 1; 2016 } 2017 verbose(VERB_ALGO, "trust anchor NXDOMAIN by signed parent"); 2018 } else if(subtype == VAL_CLASS_POSITIVE && 2019 qstate->qinfo.qtype == LDNS_RR_TYPE_DNSKEY && 2020 query_dname_compare(lookup_name, qstate->qinfo.qname) == 0) { 2021 /* is a DNSKEY so lookup a bit higher since we want to 2022 * get it from a parent or from trustanchor */ 2023 dname_remove_label(&lookup_name, &lookup_len); 2024 } 2025 2026 if(vq->rrset_skip > 0 || subtype == VAL_CLASS_CNAME || 2027 subtype == VAL_CLASS_REFERRAL) { 2028 /* extract this part of orig_msg into chase_reply for 2029 * the eventual VALIDATE stage */ 2030 val_fill_reply(vq->chase_reply, vq->orig_msg->rep, 2031 vq->rrset_skip, lookup_name, lookup_len, 2032 vq->signer_name); 2033 if(verbosity >= VERB_ALGO) 2034 log_dns_msg("chased extract", &vq->qchase, 2035 vq->chase_reply); 2036 } 2037 2038 vq->key_entry = key_cache_obtain(ve->kcache, lookup_name, lookup_len, 2039 vq->qchase.qclass, qstate->region, *qstate->env->now); 2040 2041 /* there is no key and no trust anchor */ 2042 if(vq->key_entry == NULL && anchor == NULL) { 2043 /*response isn't under a trust anchor, so we cannot validate.*/ 2044 vq->chase_reply->security = sec_status_indeterminate; 2045 update_reason_bogus(vq->chase_reply, LDNS_EDE_DNSSEC_INDETERMINATE); 2046 /* go to finished state to cache this result */ 2047 vq->state = VAL_FINISHED_STATE; 2048 return 1; 2049 } 2050 /* if not key, or if keyentry is *above* the trustanchor, i.e. 2051 * the keyentry is based on another (higher) trustanchor */ 2052 else if(vq->key_entry == NULL || (anchor && 2053 dname_strict_subdomain_c(anchor->name, vq->key_entry->name))) { 2054 /* trust anchor is an 'unsigned' trust anchor */ 2055 if(anchor && anchor->numDS == 0 && anchor->numDNSKEY == 0) { 2056 vq->chase_reply->security = sec_status_insecure; 2057 val_mark_insecure(vq->chase_reply, anchor->name, 2058 qstate->env->rrset_cache, qstate->env); 2059 lock_basic_unlock(&anchor->lock); 2060 /* go to finished state to cache this result */ 2061 vq->state = VAL_FINISHED_STATE; 2062 return 1; 2063 } 2064 /* fire off a trust anchor priming query. */ 2065 verbose(VERB_DETAIL, "prime trust anchor"); 2066 if(!prime_trust_anchor(qstate, vq, id, anchor)) { 2067 lock_basic_unlock(&anchor->lock); 2068 return val_error(qstate, id); 2069 } 2070 lock_basic_unlock(&anchor->lock); 2071 /* and otherwise, don't continue processing this event. 2072 * (it will be reactivated when the priming query returns). */ 2073 vq->state = VAL_FINDKEY_STATE; 2074 return 0; 2075 } 2076 if(anchor) { 2077 lock_basic_unlock(&anchor->lock); 2078 } 2079 2080 if(key_entry_isnull(vq->key_entry)) { 2081 /* response is under a null key, so we cannot validate 2082 * However, we do set the status to INSECURE, since it is 2083 * essentially proven insecure. */ 2084 vq->chase_reply->security = sec_status_insecure; 2085 val_mark_insecure(vq->chase_reply, vq->key_entry->name, 2086 qstate->env->rrset_cache, qstate->env); 2087 /* go to finished state to cache this result */ 2088 vq->state = VAL_FINISHED_STATE; 2089 return 1; 2090 } else if(key_entry_isbad(vq->key_entry)) { 2091 /* Bad keys should have the relevant EDE code and text */ 2092 sldns_ede_code ede = key_entry_get_reason_bogus(vq->key_entry); 2093 /* key is bad, chain is bad, reply is bogus */ 2094 errinf_dname(qstate, "key for validation", vq->key_entry->name); 2095 errinf_ede(qstate, "is marked as invalid", ede); 2096 errinf(qstate, "because of a previous"); 2097 errinf(qstate, key_entry_get_reason(vq->key_entry)); 2098 2099 /* no retries, stop bothering the authority until timeout */ 2100 vq->restart_count = ve->max_restart; 2101 vq->chase_reply->security = sec_status_bogus; 2102 update_reason_bogus(vq->chase_reply, ede); 2103 vq->state = VAL_FINISHED_STATE; 2104 return 1; 2105 } 2106 2107 /* otherwise, we have our "closest" cached key -- continue 2108 * processing in the next state. */ 2109 vq->state = VAL_FINDKEY_STATE; 2110 return 1; 2111 } 2112 2113 /** 2114 * Process the FINDKEY state. Generally this just calculates the next name 2115 * to query and either issues a DS or a DNSKEY query. It will check to see 2116 * if the correct key has already been reached, in which case it will 2117 * advance the event to the next state. 2118 * 2119 * @param qstate: query state. 2120 * @param vq: validator query state. 2121 * @param id: module id. 2122 * @return true if the event should be processed further on return, false if 2123 * not. 2124 */ 2125 static int 2126 processFindKey(struct module_qstate* qstate, struct val_qstate* vq, int id) 2127 { 2128 uint8_t* target_key_name, *current_key_name; 2129 size_t target_key_len; 2130 int strip_lab; 2131 struct module_qstate* newq = NULL; 2132 2133 log_query_info(VERB_ALGO, "validator: FindKey", &vq->qchase); 2134 /* We know that state.key_entry is not 0 or bad key -- if it were, 2135 * then previous processing should have directed this event to 2136 * a different state. 2137 * It could be an isnull key, which signals the DNSKEY failed 2138 * with retry and has to be looked up again. */ 2139 log_assert(vq->key_entry && !key_entry_isbad(vq->key_entry)); 2140 if(key_entry_isnull(vq->key_entry)) { 2141 if(!generate_request(qstate, id, vq->ds_rrset->rk.dname, 2142 vq->ds_rrset->rk.dname_len, LDNS_RR_TYPE_DNSKEY, 2143 vq->qchase.qclass, BIT_CD, &newq, 0)) { 2144 verbose(VERB_ALGO, "error generating DNSKEY request"); 2145 return val_error(qstate, id); 2146 } 2147 return 0; 2148 } 2149 2150 target_key_name = vq->signer_name; 2151 target_key_len = vq->signer_len; 2152 if(!target_key_name) { 2153 target_key_name = vq->qchase.qname; 2154 target_key_len = vq->qchase.qname_len; 2155 } 2156 2157 current_key_name = vq->key_entry->name; 2158 2159 /* If our current key entry matches our target, then we are done. */ 2160 if(query_dname_compare(target_key_name, current_key_name) == 0) { 2161 vq->state = VAL_VALIDATE_STATE; 2162 return 1; 2163 } 2164 2165 if(vq->empty_DS_name) { 2166 /* if the last empty nonterminal/emptyDS name we detected is 2167 * below the current key, use that name to make progress 2168 * along the chain of trust */ 2169 if(query_dname_compare(target_key_name, 2170 vq->empty_DS_name) == 0) { 2171 /* do not query for empty_DS_name again */ 2172 verbose(VERB_ALGO, "Cannot retrieve DS for signature"); 2173 errinf_ede(qstate, "no signatures", LDNS_EDE_RRSIGS_MISSING); 2174 errinf_origin(qstate, qstate->reply_origin); 2175 vq->chase_reply->security = sec_status_bogus; 2176 update_reason_bogus(vq->chase_reply, LDNS_EDE_RRSIGS_MISSING); 2177 vq->state = VAL_FINISHED_STATE; 2178 return 1; 2179 } 2180 current_key_name = vq->empty_DS_name; 2181 } 2182 2183 log_nametypeclass(VERB_ALGO, "current keyname", current_key_name, 2184 LDNS_RR_TYPE_DNSKEY, LDNS_RR_CLASS_IN); 2185 log_nametypeclass(VERB_ALGO, "target keyname", target_key_name, 2186 LDNS_RR_TYPE_DNSKEY, LDNS_RR_CLASS_IN); 2187 /* assert we are walking down the DNS tree */ 2188 if(!dname_subdomain_c(target_key_name, current_key_name)) { 2189 verbose(VERB_ALGO, "bad signer name"); 2190 vq->chase_reply->security = sec_status_bogus; 2191 vq->state = VAL_FINISHED_STATE; 2192 return 1; 2193 } 2194 /* so this value is >= -1 */ 2195 strip_lab = dname_count_labels(target_key_name) - 2196 dname_count_labels(current_key_name) - 1; 2197 log_assert(strip_lab >= -1); 2198 verbose(VERB_ALGO, "striplab %d", strip_lab); 2199 if(strip_lab > 0) { 2200 dname_remove_labels(&target_key_name, &target_key_len, 2201 strip_lab); 2202 } 2203 log_nametypeclass(VERB_ALGO, "next keyname", target_key_name, 2204 LDNS_RR_TYPE_DNSKEY, LDNS_RR_CLASS_IN); 2205 2206 /* The next step is either to query for the next DS, or to query 2207 * for the next DNSKEY. */ 2208 if(vq->ds_rrset) 2209 log_nametypeclass(VERB_ALGO, "DS RRset", vq->ds_rrset->rk.dname, LDNS_RR_TYPE_DS, LDNS_RR_CLASS_IN); 2210 else verbose(VERB_ALGO, "No DS RRset"); 2211 2212 if(vq->ds_rrset && query_dname_compare(vq->ds_rrset->rk.dname, 2213 vq->key_entry->name) != 0) { 2214 if(!generate_request(qstate, id, vq->ds_rrset->rk.dname, 2215 vq->ds_rrset->rk.dname_len, LDNS_RR_TYPE_DNSKEY, 2216 vq->qchase.qclass, BIT_CD, &newq, 0)) { 2217 verbose(VERB_ALGO, "error generating DNSKEY request"); 2218 return val_error(qstate, id); 2219 } 2220 return 0; 2221 } 2222 2223 if(!vq->ds_rrset || query_dname_compare(vq->ds_rrset->rk.dname, 2224 target_key_name) != 0) { 2225 /* check if there is a cache entry : pick up an NSEC if 2226 * there is no DS, check if that NSEC has DS-bit unset, and 2227 * thus can disprove the secure delegation we seek. 2228 * We can then use that NSEC even in the absence of a SOA 2229 * record that would be required by the iterator to supply 2230 * a completely protocol-correct response. 2231 * Uses negative cache for NSEC3 lookup of DS responses. */ 2232 /* only if cache not blacklisted, of course */ 2233 struct dns_msg* msg; 2234 int suspend; 2235 if(vq->sub_ds_msg) { 2236 /* We have a suspended DS reply from a sub-query; 2237 * process it. */ 2238 verbose(VERB_ALGO, "Process suspended sub DS response"); 2239 msg = vq->sub_ds_msg; 2240 process_ds_response(qstate, vq, id, LDNS_RCODE_NOERROR, 2241 msg, &msg->qinfo, NULL, &suspend, NULL); 2242 if(suspend) { 2243 /* we'll come back here later to continue */ 2244 if(!validate_suspend_setup_timer(qstate, vq, 2245 id, VAL_FINDKEY_STATE)) 2246 return val_error(qstate, id); 2247 return 0; 2248 } 2249 vq->sub_ds_msg = NULL; 2250 return 1; /* continue processing ds-response results */ 2251 } else if(!qstate->blacklist && !vq->chain_blacklist && 2252 (msg=val_find_DS(qstate->env, target_key_name, 2253 target_key_len, vq->qchase.qclass, qstate->region, 2254 vq->key_entry->name)) ) { 2255 verbose(VERB_ALGO, "Process cached DS response"); 2256 process_ds_response(qstate, vq, id, LDNS_RCODE_NOERROR, 2257 msg, &msg->qinfo, NULL, &suspend, NULL); 2258 if(suspend) { 2259 /* we'll come back here later to continue */ 2260 if(!validate_suspend_setup_timer(qstate, vq, 2261 id, VAL_FINDKEY_STATE)) 2262 return val_error(qstate, id); 2263 return 0; 2264 } 2265 return 1; /* continue processing ds-response results */ 2266 } 2267 if(!generate_request(qstate, id, target_key_name, 2268 target_key_len, LDNS_RR_TYPE_DS, vq->qchase.qclass, 2269 BIT_CD, &newq, 0)) { 2270 verbose(VERB_ALGO, "error generating DS request"); 2271 return val_error(qstate, id); 2272 } 2273 return 0; 2274 } 2275 2276 /* Otherwise, it is time to query for the DNSKEY */ 2277 if(!generate_request(qstate, id, vq->ds_rrset->rk.dname, 2278 vq->ds_rrset->rk.dname_len, LDNS_RR_TYPE_DNSKEY, 2279 vq->qchase.qclass, BIT_CD, &newq, 0)) { 2280 verbose(VERB_ALGO, "error generating DNSKEY request"); 2281 return val_error(qstate, id); 2282 } 2283 2284 return 0; 2285 } 2286 2287 /** 2288 * Process the VALIDATE stage, the init and findkey stages are finished, 2289 * and the right keys are available to validate the response. 2290 * Or, there are no keys available, in order to invalidate the response. 2291 * 2292 * After validation, the status is recorded in the message and rrsets, 2293 * and finished state is started. 2294 * 2295 * @param qstate: query state. 2296 * @param vq: validator query state. 2297 * @param ve: validator shared global environment. 2298 * @param id: module id. 2299 * @return true if the event should be processed further on return, false if 2300 * not. 2301 */ 2302 static int 2303 processValidate(struct module_qstate* qstate, struct val_qstate* vq, 2304 struct val_env* ve, int id) 2305 { 2306 enum val_classification subtype; 2307 int rcode, suspend, nsec3_calculations = 0; 2308 2309 if(!vq->key_entry) { 2310 verbose(VERB_ALGO, "validate: no key entry, failed"); 2311 return val_error(qstate, id); 2312 } 2313 2314 /* This is the default next state. */ 2315 vq->state = VAL_FINISHED_STATE; 2316 2317 /* Unsigned responses must be underneath a "null" key entry.*/ 2318 if(key_entry_isnull(vq->key_entry)) { 2319 verbose(VERB_DETAIL, "Verified that %sresponse is INSECURE", 2320 vq->signer_name?"":"unsigned "); 2321 vq->chase_reply->security = sec_status_insecure; 2322 val_mark_insecure(vq->chase_reply, vq->key_entry->name, 2323 qstate->env->rrset_cache, qstate->env); 2324 key_cache_insert(ve->kcache, vq->key_entry, 2325 qstate->env->cfg->val_log_level >= 2); 2326 return 1; 2327 } 2328 2329 if(key_entry_isbad(vq->key_entry)) { 2330 log_nametypeclass(VERB_DETAIL, "Could not establish a chain " 2331 "of trust to keys for", vq->key_entry->name, 2332 LDNS_RR_TYPE_DNSKEY, vq->key_entry->key_class); 2333 vq->chase_reply->security = sec_status_bogus; 2334 update_reason_bogus(vq->chase_reply, 2335 key_entry_get_reason_bogus(vq->key_entry)); 2336 errinf_ede(qstate, "while building chain of trust", 2337 key_entry_get_reason_bogus(vq->key_entry)); 2338 if(!val_can_restart(qstate, vq, ve)) 2339 key_cache_insert(ve->kcache, vq->key_entry, 2340 qstate->env->cfg->val_log_level >= 2); 2341 return 1; 2342 } 2343 2344 /* signerName being null is the indicator that this response was 2345 * unsigned */ 2346 if(vq->signer_name == NULL) { 2347 log_query_info(VERB_ALGO, "processValidate: state has no " 2348 "signer name", &vq->qchase); 2349 verbose(VERB_DETAIL, "Could not establish validation of " 2350 "INSECURE status of unsigned response."); 2351 errinf_ede(qstate, "no signatures", LDNS_EDE_RRSIGS_MISSING); 2352 errinf_origin(qstate, qstate->reply_origin); 2353 vq->chase_reply->security = sec_status_bogus; 2354 update_reason_bogus(vq->chase_reply, LDNS_EDE_RRSIGS_MISSING); 2355 return 1; 2356 } 2357 subtype = val_classify_response(qstate->query_flags, &qstate->qinfo, 2358 &vq->qchase, vq->orig_msg->rep, vq->rrset_skip); 2359 if(subtype != VAL_CLASS_REFERRAL) 2360 remove_spurious_authority(vq->chase_reply, vq->orig_msg->rep); 2361 if(subtype == VAL_CLASS_ANY) 2362 shorten_answer_any(vq->chase_reply, vq->orig_msg->rep, 2363 vq->rrset_skip, MAX_RRSETS_ANY_VALIDATED); 2364 2365 /* check signatures in the message; 2366 * answer and authority must be valid, additional is only checked. */ 2367 if(!validate_msg_signatures(qstate, vq, qstate->env, ve, 2368 vq->chase_reply, vq->key_entry, &suspend)) { 2369 if(suspend) { 2370 if(!validate_suspend_setup_timer(qstate, vq, 2371 id, VAL_VALIDATE_STATE)) 2372 return val_error(qstate, id); 2373 return 0; 2374 } 2375 /* workaround bad recursor out there that truncates (even 2376 * with EDNS4k) to 512 by removing RRSIG from auth section 2377 * for positive replies*/ 2378 if((subtype == VAL_CLASS_POSITIVE || subtype == VAL_CLASS_ANY 2379 || subtype == VAL_CLASS_CNAME) && 2380 detect_wrongly_truncated(vq->orig_msg->rep)) { 2381 /* truncate the message some more */ 2382 vq->orig_msg->rep->ns_numrrsets = 0; 2383 vq->orig_msg->rep->ar_numrrsets = 0; 2384 vq->orig_msg->rep->rrset_count = 2385 vq->orig_msg->rep->an_numrrsets; 2386 vq->chase_reply->ns_numrrsets = 0; 2387 vq->chase_reply->ar_numrrsets = 0; 2388 vq->chase_reply->rrset_count = 2389 vq->chase_reply->an_numrrsets; 2390 qstate->errinf = NULL; 2391 } 2392 else { 2393 verbose(VERB_DETAIL, "Validate: message contains " 2394 "bad rrsets"); 2395 return 1; 2396 } 2397 } 2398 2399 switch(subtype) { 2400 case VAL_CLASS_POSITIVE: 2401 verbose(VERB_ALGO, "Validating a positive response"); 2402 validate_positive_response(qstate->env, ve, 2403 &vq->qchase, vq->chase_reply, vq->key_entry, 2404 qstate, vq, &nsec3_calculations, &suspend); 2405 if(suspend) { 2406 if(!validate_suspend_setup_timer(qstate, 2407 vq, id, VAL_VALIDATE_STATE)) 2408 return val_error(qstate, id); 2409 return 0; 2410 } 2411 verbose(VERB_DETAIL, "validate(positive): %s", 2412 sec_status_to_string( 2413 vq->chase_reply->security)); 2414 break; 2415 2416 case VAL_CLASS_NODATA: 2417 verbose(VERB_ALGO, "Validating a nodata response"); 2418 validate_nodata_response(qstate->env, ve, 2419 &vq->qchase, vq->chase_reply, vq->key_entry, 2420 qstate, vq, &nsec3_calculations, &suspend); 2421 if(suspend) { 2422 if(!validate_suspend_setup_timer(qstate, 2423 vq, id, VAL_VALIDATE_STATE)) 2424 return val_error(qstate, id); 2425 return 0; 2426 } 2427 verbose(VERB_DETAIL, "validate(nodata): %s", 2428 sec_status_to_string( 2429 vq->chase_reply->security)); 2430 break; 2431 2432 case VAL_CLASS_NAMEERROR: 2433 rcode = (int)FLAGS_GET_RCODE(vq->orig_msg->rep->flags); 2434 verbose(VERB_ALGO, "Validating a nxdomain response"); 2435 validate_nameerror_response(qstate->env, ve, 2436 &vq->qchase, vq->chase_reply, vq->key_entry, &rcode, 2437 qstate, vq, &nsec3_calculations, &suspend); 2438 if(suspend) { 2439 if(!validate_suspend_setup_timer(qstate, 2440 vq, id, VAL_VALIDATE_STATE)) 2441 return val_error(qstate, id); 2442 return 0; 2443 } 2444 verbose(VERB_DETAIL, "validate(nxdomain): %s", 2445 sec_status_to_string( 2446 vq->chase_reply->security)); 2447 FLAGS_SET_RCODE(vq->orig_msg->rep->flags, rcode); 2448 FLAGS_SET_RCODE(vq->chase_reply->flags, rcode); 2449 break; 2450 2451 case VAL_CLASS_CNAME: 2452 verbose(VERB_ALGO, "Validating a cname response"); 2453 validate_cname_response(qstate->env, ve, 2454 &vq->qchase, vq->chase_reply, vq->key_entry, 2455 qstate, vq, &nsec3_calculations, &suspend); 2456 if(suspend) { 2457 if(!validate_suspend_setup_timer(qstate, 2458 vq, id, VAL_VALIDATE_STATE)) 2459 return val_error(qstate, id); 2460 return 0; 2461 } 2462 verbose(VERB_DETAIL, "validate(cname): %s", 2463 sec_status_to_string( 2464 vq->chase_reply->security)); 2465 break; 2466 2467 case VAL_CLASS_CNAMENOANSWER: 2468 verbose(VERB_ALGO, "Validating a cname noanswer " 2469 "response"); 2470 validate_cname_noanswer_response(qstate->env, ve, 2471 &vq->qchase, vq->chase_reply, vq->key_entry, 2472 qstate, vq, &nsec3_calculations, &suspend); 2473 if(suspend) { 2474 if(!validate_suspend_setup_timer(qstate, 2475 vq, id, VAL_VALIDATE_STATE)) 2476 return val_error(qstate, id); 2477 return 0; 2478 } 2479 verbose(VERB_DETAIL, "validate(cname_noanswer): %s", 2480 sec_status_to_string( 2481 vq->chase_reply->security)); 2482 break; 2483 2484 case VAL_CLASS_REFERRAL: 2485 verbose(VERB_ALGO, "Validating a referral response"); 2486 validate_referral_response(qstate->env, vq->chase_reply); 2487 verbose(VERB_DETAIL, "validate(referral): %s", 2488 sec_status_to_string( 2489 vq->chase_reply->security)); 2490 break; 2491 2492 case VAL_CLASS_ANY: 2493 verbose(VERB_ALGO, "Validating a positive ANY " 2494 "response"); 2495 validate_any_response(qstate->env, ve, &vq->qchase, 2496 vq->chase_reply, vq->key_entry, qstate, vq, 2497 &nsec3_calculations, &suspend); 2498 if(suspend) { 2499 if(!validate_suspend_setup_timer(qstate, 2500 vq, id, VAL_VALIDATE_STATE)) 2501 return val_error(qstate, id); 2502 return 0; 2503 } 2504 verbose(VERB_DETAIL, "validate(positive_any): %s", 2505 sec_status_to_string( 2506 vq->chase_reply->security)); 2507 break; 2508 2509 default: 2510 log_err("validate: unhandled response subtype: %d", 2511 subtype); 2512 } 2513 if(vq->chase_reply->security == sec_status_bogus) { 2514 if(subtype == VAL_CLASS_POSITIVE) 2515 errinf(qstate, "wildcard"); 2516 else errinf(qstate, val_classification_to_string(subtype)); 2517 errinf(qstate, "proof failed"); 2518 errinf_origin(qstate, qstate->reply_origin); 2519 } 2520 2521 return 1; 2522 } 2523 2524 /** 2525 * The Finished state. The validation status (good or bad) has been determined. 2526 * 2527 * @param qstate: query state. 2528 * @param vq: validator query state. 2529 * @param ve: validator shared global environment. 2530 * @param id: module id. 2531 * @return true if the event should be processed further on return, false if 2532 * not. 2533 */ 2534 static int 2535 processFinished(struct module_qstate* qstate, struct val_qstate* vq, 2536 struct val_env* ve, int id) 2537 { 2538 enum val_classification subtype = val_classify_response( 2539 qstate->query_flags, &qstate->qinfo, &vq->qchase, 2540 vq->orig_msg->rep, vq->rrset_skip); 2541 2542 /* store overall validation result in orig_msg */ 2543 if(vq->rrset_skip == 0) { 2544 vq->orig_msg->rep->security = vq->chase_reply->security; 2545 update_reason_bogus(vq->orig_msg->rep, vq->chase_reply->reason_bogus); 2546 } else if(subtype != VAL_CLASS_REFERRAL || 2547 vq->rrset_skip < vq->orig_msg->rep->an_numrrsets + 2548 vq->orig_msg->rep->ns_numrrsets) { 2549 /* ignore sec status of additional section if a referral 2550 * type message skips there and 2551 * use the lowest security status as end result. */ 2552 if(vq->chase_reply->security < vq->orig_msg->rep->security) { 2553 vq->orig_msg->rep->security = 2554 vq->chase_reply->security; 2555 update_reason_bogus(vq->orig_msg->rep, vq->chase_reply->reason_bogus); 2556 } 2557 } 2558 2559 if(subtype == VAL_CLASS_REFERRAL) { 2560 if(qstate->env->cfg->val_clean_additional) { 2561 /* for a referral, move to next unchecked rrset and check it*/ 2562 vq->rrset_skip = val_next_unchecked(vq->orig_msg->rep, 2563 vq->rrset_skip); 2564 if(vq->rrset_skip < vq->orig_msg->rep->rrset_count) { 2565 /* and restart for this rrset */ 2566 verbose(VERB_ALGO, "validator: go to next rrset"); 2567 vq->chase_reply->security = sec_status_unchecked; 2568 vq->state = VAL_INIT_STATE; 2569 return 1; 2570 } 2571 } 2572 /* referral chase is done */ 2573 } 2574 if(vq->chase_reply->security != sec_status_bogus && 2575 subtype == VAL_CLASS_CNAME) { 2576 /* chase the CNAME; process next part of the message */ 2577 if(!val_chase_cname(&vq->qchase, vq->orig_msg->rep, 2578 &vq->rrset_skip)) { 2579 verbose(VERB_ALGO, "validator: failed to chase CNAME"); 2580 vq->orig_msg->rep->security = sec_status_bogus; 2581 update_reason_bogus(vq->orig_msg->rep, LDNS_EDE_DNSSEC_BOGUS); 2582 } else { 2583 /* restart process for new qchase at rrset_skip */ 2584 log_query_info(VERB_ALGO, "validator: chased to", 2585 &vq->qchase); 2586 vq->chase_reply->security = sec_status_unchecked; 2587 vq->state = VAL_INIT_STATE; 2588 return 1; 2589 } 2590 } 2591 2592 if(vq->orig_msg->rep->security == sec_status_secure) { 2593 /* If the message is secure, check that all rrsets are 2594 * secure (i.e. some inserted RRset for CNAME chain with 2595 * a different signer name). And drop additional rrsets 2596 * that are not secure (if clean-additional option is set) */ 2597 /* this may cause the msg to be marked bogus */ 2598 val_check_nonsecure(qstate->env, vq->orig_msg->rep); 2599 if(vq->orig_msg->rep->security == sec_status_secure) { 2600 log_query_info(VERB_DETAIL, "validation success", 2601 &qstate->qinfo); 2602 if(!qstate->no_cache_store) { 2603 val_neg_addreply(qstate->env->neg_cache, 2604 vq->orig_msg->rep); 2605 } 2606 } 2607 } 2608 2609 /* if the result is bogus - set message ttl to bogus ttl to avoid 2610 * endless bogus revalidation */ 2611 if(vq->orig_msg->rep->security == sec_status_bogus) { 2612 struct msgreply_entry* e; 2613 2614 /* see if we can try again to fetch data */ 2615 if(val_can_restart(qstate, vq, ve)) { 2616 verbose(VERB_ALGO, "validation failed, " 2617 "blacklist and retry to fetch data"); 2618 val_blacklist(&qstate->blacklist, qstate->region, 2619 qstate->reply_origin, 0); 2620 qstate->reply_origin = NULL; 2621 qstate->errinf = NULL; 2622 val_restart(vq); 2623 verbose(VERB_ALGO, "pass back to next module"); 2624 qstate->ext_state[id] = module_restart_next; 2625 return 0; 2626 } 2627 2628 if(qstate->env->cfg->serve_expired && 2629 (e=msg_cache_lookup(qstate->env, qstate->qinfo.qname, 2630 qstate->qinfo.qname_len, qstate->qinfo.qtype, 2631 qstate->qinfo.qclass, qstate->query_flags, 2632 0 /*now; allow expired*/, 2633 1 /*wr; we may update the data*/))) { 2634 struct reply_info* rep = (struct reply_info*)e->entry.data; 2635 if(rep && rep->security > sec_status_bogus && 2636 (!qstate->env->cfg->serve_expired_ttl || 2637 qstate->env->cfg->serve_expired_ttl_reset || 2638 *qstate->env->now <= rep->serve_expired_ttl)) { 2639 verbose(VERB_ALGO, "validation failed but " 2640 "previously cached valid response " 2641 "exists; set serve-expired-norec-ttl " 2642 "for response in cache"); 2643 rep->serve_expired_norec_ttl = NORR_TTL + 2644 *qstate->env->now; 2645 if(qstate->env->cfg->serve_expired_ttl_reset && 2646 *qstate->env->now + qstate->env->cfg->serve_expired_ttl 2647 > rep->serve_expired_ttl) { 2648 verbose(VERB_ALGO, "reset serve-expired-ttl for " 2649 "valid response in cache"); 2650 rep->serve_expired_ttl = *qstate->env->now + 2651 qstate->env->cfg->serve_expired_ttl; 2652 } 2653 /* Return an error response. 2654 * If serve-expired-client-timeout is enabled, 2655 * the client-timeout logic will try to find an 2656 * (expired) answer in the cache as last 2657 * resort. If it is not enabled, expired 2658 * answers are already used before the mesh 2659 * activation. */ 2660 qstate->return_rcode = LDNS_RCODE_SERVFAIL; 2661 qstate->return_msg = NULL; 2662 qstate->ext_state[id] = module_finished; 2663 lock_rw_unlock(&e->entry.lock); 2664 return 0; 2665 } 2666 lock_rw_unlock(&e->entry.lock); 2667 } 2668 2669 vq->orig_msg->rep->ttl = ve->bogus_ttl; 2670 vq->orig_msg->rep->prefetch_ttl = 2671 PREFETCH_TTL_CALC(vq->orig_msg->rep->ttl); 2672 vq->orig_msg->rep->serve_expired_ttl = 2673 vq->orig_msg->rep->ttl + qstate->env->cfg->serve_expired_ttl; 2674 if((qstate->env->cfg->val_log_level >= 1 || 2675 qstate->env->cfg->log_servfail) && 2676 !qstate->env->cfg->val_log_squelch) { 2677 if(qstate->env->cfg->val_log_level < 2 && 2678 !qstate->env->cfg->log_servfail) 2679 log_query_info(NO_VERBOSE, "validation failure", 2680 &qstate->qinfo); 2681 else { 2682 char* err_str = errinf_to_str_bogus(qstate, 2683 qstate->region); 2684 if(err_str) { 2685 log_info("%s", err_str); 2686 vq->orig_msg->rep->reason_bogus_str = err_str; 2687 } 2688 } 2689 } 2690 /* 2691 * If set, the validator will not make messages bogus, instead 2692 * indeterminate is issued, so that no clients receive SERVFAIL. 2693 * This allows an operator to run validation 'shadow' without 2694 * hurting responses to clients. 2695 */ 2696 /* If we are in permissive mode, bogus gets indeterminate */ 2697 if(qstate->env->cfg->val_permissive_mode) 2698 vq->orig_msg->rep->security = sec_status_indeterminate; 2699 } 2700 2701 if(vq->orig_msg->rep->security == sec_status_secure && 2702 qstate->env->cfg->root_key_sentinel && 2703 (qstate->qinfo.qtype == LDNS_RR_TYPE_A || 2704 qstate->qinfo.qtype == LDNS_RR_TYPE_AAAA)) { 2705 char* keytag_start; 2706 uint16_t keytag; 2707 if(*qstate->qinfo.qname == strlen(SENTINEL_IS) + 2708 SENTINEL_KEYTAG_LEN && 2709 dname_lab_startswith(qstate->qinfo.qname, SENTINEL_IS, 2710 &keytag_start)) { 2711 if(sentinel_get_keytag(keytag_start, &keytag) && 2712 !anchor_has_keytag(qstate->env->anchors, 2713 (uint8_t*)"", 1, 0, vq->qchase.qclass, keytag)) { 2714 vq->orig_msg->rep->security = 2715 sec_status_secure_sentinel_fail; 2716 } 2717 } else if(*qstate->qinfo.qname == strlen(SENTINEL_NOT) + 2718 SENTINEL_KEYTAG_LEN && 2719 dname_lab_startswith(qstate->qinfo.qname, SENTINEL_NOT, 2720 &keytag_start)) { 2721 if(sentinel_get_keytag(keytag_start, &keytag) && 2722 anchor_has_keytag(qstate->env->anchors, 2723 (uint8_t*)"", 1, 0, vq->qchase.qclass, keytag)) { 2724 vq->orig_msg->rep->security = 2725 sec_status_secure_sentinel_fail; 2726 } 2727 } 2728 } 2729 2730 /* Update rep->reason_bogus as it is the one being cached */ 2731 update_reason_bogus(vq->orig_msg->rep, errinf_to_reason_bogus(qstate)); 2732 if(vq->orig_msg->rep->security != sec_status_bogus && 2733 vq->orig_msg->rep->security != sec_status_secure_sentinel_fail 2734 && vq->orig_msg->rep->reason_bogus == LDNS_EDE_DNSSEC_BOGUS) { 2735 /* Not interested in any DNSSEC EDE here, validator by default 2736 * uses LDNS_EDE_DNSSEC_BOGUS; 2737 * TODO revisit default value for the module */ 2738 vq->orig_msg->rep->reason_bogus = LDNS_EDE_NONE; 2739 } 2740 2741 /* store results in cache */ 2742 if((qstate->query_flags&BIT_RD)) { 2743 /* if secure, this will override cache anyway, no need 2744 * to check if from parentNS */ 2745 if(!qstate->no_cache_store) { 2746 if(!dns_cache_store(qstate->env, &vq->orig_msg->qinfo, 2747 vq->orig_msg->rep, 0, qstate->prefetch_leeway, 2748 0, qstate->region, qstate->query_flags, 2749 qstate->qstarttime, qstate->is_valrec)) { 2750 log_err("out of memory caching validator results"); 2751 } 2752 } 2753 } else { 2754 /* for a referral, store the verified RRsets */ 2755 /* and this does not get prefetched, so no leeway */ 2756 if(!dns_cache_store(qstate->env, &vq->orig_msg->qinfo, 2757 vq->orig_msg->rep, 1, 0, 0, qstate->region, 2758 qstate->query_flags, qstate->qstarttime, 2759 qstate->is_valrec)) { 2760 log_err("out of memory caching validator results"); 2761 } 2762 } 2763 qstate->return_rcode = LDNS_RCODE_NOERROR; 2764 qstate->return_msg = vq->orig_msg; 2765 qstate->ext_state[id] = module_finished; 2766 return 0; 2767 } 2768 2769 /** 2770 * Handle validator state. 2771 * If a method returns true, the next state is started. If false, then 2772 * processing will stop. 2773 * @param qstate: query state. 2774 * @param vq: validator query state. 2775 * @param ve: validator shared global environment. 2776 * @param id: module id. 2777 */ 2778 static void 2779 val_handle(struct module_qstate* qstate, struct val_qstate* vq, 2780 struct val_env* ve, int id) 2781 { 2782 int cont = 1; 2783 while(cont) { 2784 verbose(VERB_ALGO, "val handle processing q with state %s", 2785 val_state_to_string(vq->state)); 2786 switch(vq->state) { 2787 case VAL_INIT_STATE: 2788 cont = processInit(qstate, vq, ve, id); 2789 break; 2790 case VAL_FINDKEY_STATE: 2791 cont = processFindKey(qstate, vq, id); 2792 break; 2793 case VAL_VALIDATE_STATE: 2794 cont = processValidate(qstate, vq, ve, id); 2795 break; 2796 case VAL_FINISHED_STATE: 2797 cont = processFinished(qstate, vq, ve, id); 2798 break; 2799 default: 2800 log_warn("validator: invalid state %d", 2801 vq->state); 2802 cont = 0; 2803 break; 2804 } 2805 } 2806 } 2807 2808 void 2809 val_operate(struct module_qstate* qstate, enum module_ev event, int id, 2810 struct outbound_entry* outbound) 2811 { 2812 struct val_env* ve = (struct val_env*)qstate->env->modinfo[id]; 2813 struct val_qstate* vq = (struct val_qstate*)qstate->minfo[id]; 2814 verbose(VERB_QUERY, "validator[module %d] operate: extstate:%s " 2815 "event:%s", id, strextstate(qstate->ext_state[id]), 2816 strmodulevent(event)); 2817 log_query_info(VERB_QUERY, "validator operate: query", 2818 &qstate->qinfo); 2819 if(vq && qstate->qinfo.qname != vq->qchase.qname) 2820 log_query_info(VERB_QUERY, "validator operate: chased to", 2821 &vq->qchase); 2822 (void)outbound; 2823 if(event == module_event_new || 2824 (event == module_event_pass && vq == NULL)) { 2825 2826 /* pass request to next module, to get it */ 2827 verbose(VERB_ALGO, "validator: pass to next module"); 2828 qstate->ext_state[id] = module_wait_module; 2829 return; 2830 } 2831 if(event == module_event_moddone) { 2832 /* check if validation is needed */ 2833 verbose(VERB_ALGO, "validator: nextmodule returned"); 2834 2835 if(!needs_validation(qstate, qstate->return_rcode, 2836 qstate->return_msg)) { 2837 /* no need to validate this */ 2838 /* For valrec responses, leave at sec_status_unchecked, 2839 * no security status has been requested for it. */ 2840 if(qstate->return_msg && !qstate->is_valrec) 2841 qstate->return_msg->rep->security = 2842 sec_status_indeterminate; 2843 qstate->ext_state[id] = module_finished; 2844 return; 2845 } 2846 if(already_validated(qstate->return_msg)) { 2847 qstate->ext_state[id] = module_finished; 2848 return; 2849 } 2850 if(qstate->rpz_applied) { 2851 verbose(VERB_ALGO, "rpz applied, mark it as insecure"); 2852 if(qstate->return_msg) 2853 qstate->return_msg->rep->security = 2854 sec_status_insecure; 2855 qstate->ext_state[id] = module_finished; 2856 return; 2857 } 2858 /* qclass ANY should have validation result from spawned 2859 * queries. If we get here, it is bogus or an internal error */ 2860 if(qstate->qinfo.qclass == LDNS_RR_CLASS_ANY) { 2861 verbose(VERB_ALGO, "cannot validate classANY: bogus"); 2862 if(qstate->return_msg) { 2863 qstate->return_msg->rep->security = 2864 sec_status_bogus; 2865 update_reason_bogus(qstate->return_msg->rep, LDNS_EDE_DNSSEC_BOGUS); 2866 } 2867 qstate->ext_state[id] = module_finished; 2868 return; 2869 } 2870 /* create state to start validation */ 2871 qstate->ext_state[id] = module_error; /* override this */ 2872 if(!vq) { 2873 vq = val_new(qstate, id); 2874 if(!vq) { 2875 log_err("validator: malloc failure"); 2876 qstate->ext_state[id] = module_error; 2877 return; 2878 } 2879 } else if(!vq->orig_msg) { 2880 if(!val_new_getmsg(qstate, vq)) { 2881 log_err("validator: malloc failure"); 2882 qstate->ext_state[id] = module_error; 2883 return; 2884 } 2885 } 2886 val_handle(qstate, vq, ve, id); 2887 return; 2888 } 2889 if(event == module_event_pass) { 2890 qstate->ext_state[id] = module_error; /* override this */ 2891 /* continue processing, since val_env exists */ 2892 val_handle(qstate, vq, ve, id); 2893 return; 2894 } 2895 log_err("validator: bad event %s", strmodulevent(event)); 2896 qstate->ext_state[id] = module_error; 2897 return; 2898 } 2899 2900 /** 2901 * Evaluate the response to a priming request. 2902 * 2903 * @param dnskey_rrset: DNSKEY rrset (can be NULL if none) in prime reply. 2904 * (this rrset is allocated in the wrong region, not the qstate). 2905 * @param ta: trust anchor. 2906 * @param qstate: qstate that needs key. 2907 * @param vq: validator qstate. 2908 * @param id: module id. 2909 * @param sub_qstate: the sub query state, that is the lookup that fetched 2910 * the trust anchor data, it contains error information for the answer. 2911 * @return new key entry or NULL on allocation failure. 2912 * The key entry will either contain a validated DNSKEY rrset, or 2913 * represent a Null key (query failed, but validation did not), or a 2914 * Bad key (validation failed). 2915 */ 2916 static struct key_entry_key* 2917 primeResponseToKE(struct ub_packed_rrset_key* dnskey_rrset, 2918 struct trust_anchor* ta, struct module_qstate* qstate, 2919 struct val_qstate* vq, int id, struct module_qstate* sub_qstate) 2920 { 2921 struct val_env* ve = (struct val_env*)qstate->env->modinfo[id]; 2922 struct key_entry_key* kkey = NULL; 2923 enum sec_status sec = sec_status_unchecked; 2924 char reasonbuf[256]; 2925 char* reason = NULL; 2926 sldns_ede_code reason_bogus = LDNS_EDE_DNSSEC_BOGUS; 2927 int downprot = qstate->env->cfg->harden_algo_downgrade; 2928 2929 if(!dnskey_rrset) { 2930 char* err = errinf_to_str_misc(sub_qstate); 2931 char rstr[1024]; 2932 log_nametypeclass(VERB_OPS, "failed to prime trust anchor -- " 2933 "could not fetch DNSKEY rrset", 2934 ta->name, LDNS_RR_TYPE_DNSKEY, ta->dclass); 2935 reason_bogus = LDNS_EDE_DNSKEY_MISSING; 2936 if(!err) { 2937 snprintf(rstr, sizeof(rstr), "no DNSKEY rrset"); 2938 } else { 2939 snprintf(rstr, sizeof(rstr), "no DNSKEY rrset " 2940 "[%s]", err); 2941 } 2942 if(qstate->env->cfg->harden_dnssec_stripped) { 2943 errinf_ede(qstate, rstr, reason_bogus); 2944 kkey = key_entry_create_bad(qstate->region, ta->name, 2945 ta->namelen, ta->dclass, BOGUS_KEY_TTL, 2946 reason_bogus, rstr, *qstate->env->now); 2947 } else kkey = key_entry_create_null(qstate->region, ta->name, 2948 ta->namelen, ta->dclass, NULL_KEY_TTL, 2949 reason_bogus, rstr, *qstate->env->now); 2950 if(!kkey) { 2951 log_err("out of memory: allocate fail prime key"); 2952 return NULL; 2953 } 2954 return kkey; 2955 } 2956 /* attempt to verify with trust anchor DS and DNSKEY */ 2957 kkey = val_verify_new_DNSKEYs_with_ta(qstate->region, qstate->env, ve, 2958 dnskey_rrset, ta->ds_rrset, ta->dnskey_rrset, downprot, 2959 &reason, &reason_bogus, qstate, vq, reasonbuf, 2960 sizeof(reasonbuf)); 2961 if(!kkey) { 2962 log_err("out of memory: verifying prime TA"); 2963 return NULL; 2964 } 2965 if(key_entry_isgood(kkey)) 2966 sec = sec_status_secure; 2967 else 2968 sec = sec_status_bogus; 2969 verbose(VERB_DETAIL, "validate keys with anchor(DS): %s", 2970 sec_status_to_string(sec)); 2971 2972 if(sec != sec_status_secure) { 2973 log_nametypeclass(VERB_OPS, "failed to prime trust anchor -- " 2974 "DNSKEY rrset is not secure", 2975 ta->name, LDNS_RR_TYPE_DNSKEY, ta->dclass); 2976 /* NOTE: in this case, we should probably reject the trust 2977 * anchor for longer, perhaps forever. */ 2978 if(qstate->env->cfg->harden_dnssec_stripped) { 2979 errinf_ede(qstate, reason, reason_bogus); 2980 kkey = key_entry_create_bad(qstate->region, ta->name, 2981 ta->namelen, ta->dclass, BOGUS_KEY_TTL, 2982 reason_bogus, reason, 2983 *qstate->env->now); 2984 } else kkey = key_entry_create_null(qstate->region, ta->name, 2985 ta->namelen, ta->dclass, NULL_KEY_TTL, 2986 reason_bogus, reason, 2987 *qstate->env->now); 2988 if(!kkey) { 2989 log_err("out of memory: allocate null prime key"); 2990 return NULL; 2991 } 2992 return kkey; 2993 } 2994 2995 log_nametypeclass(VERB_DETAIL, "Successfully primed trust anchor", 2996 ta->name, LDNS_RR_TYPE_DNSKEY, ta->dclass); 2997 return kkey; 2998 } 2999 3000 /** 3001 * In inform supers, with the resulting message and rcode and the current 3002 * keyset in the super state, validate the DS response, returning a KeyEntry. 3003 * 3004 * @param qstate: query state that is validating and asked for a DS. 3005 * @param vq: validator query state 3006 * @param id: module id. 3007 * @param rcode: rcode result value. 3008 * @param msg: result message (if rcode is OK). 3009 * @param qinfo: from the sub query state, query info. 3010 * @param ke: the key entry to return. It returns 3011 * is_bad if the DS response fails to validate, is_null if the 3012 * DS response indicated an end to secure space, is_good if the DS 3013 * validated. It returns ke=NULL if the DS response indicated that the 3014 * request wasn't a delegation point. 3015 * @param sub_qstate: the sub query state, that is the lookup that fetched 3016 * the trust anchor data, it contains error information for the answer. 3017 * Can be NULL. 3018 * @return 3019 * 0 on success, 3020 * 1 on servfail error (malloc failure), 3021 * 2 on NSEC3 suspend. 3022 */ 3023 static int 3024 ds_response_to_ke(struct module_qstate* qstate, struct val_qstate* vq, 3025 int id, int rcode, struct dns_msg* msg, struct query_info* qinfo, 3026 struct key_entry_key** ke, struct module_qstate* sub_qstate) 3027 { 3028 struct val_env* ve = (struct val_env*)qstate->env->modinfo[id]; 3029 char reasonbuf[256]; 3030 char* reason = NULL; 3031 sldns_ede_code reason_bogus = LDNS_EDE_DNSSEC_BOGUS; 3032 enum val_classification subtype; 3033 int verified; 3034 if(rcode != LDNS_RCODE_NOERROR) { 3035 char rc[16]; 3036 rc[0]=0; 3037 (void)sldns_wire2str_rcode_buf(rcode, rc, sizeof(rc)); 3038 /* errors here pretty much break validation */ 3039 verbose(VERB_DETAIL, "DS response was error, thus bogus"); 3040 errinf(qstate, rc); 3041 reason = "no DS"; 3042 if(sub_qstate) { 3043 char* err = errinf_to_str_misc(sub_qstate); 3044 if(err) { 3045 char buf[1024]; 3046 snprintf(buf, sizeof(buf), "[%s]", err); 3047 errinf(qstate, buf); 3048 } 3049 } 3050 reason_bogus = LDNS_EDE_NETWORK_ERROR; 3051 errinf_ede(qstate, reason, reason_bogus); 3052 goto return_bogus; 3053 } 3054 3055 subtype = val_classify_response(BIT_RD, qinfo, qinfo, msg->rep, 0); 3056 if(subtype == VAL_CLASS_POSITIVE) { 3057 struct ub_packed_rrset_key* ds; 3058 enum sec_status sec; 3059 ds = reply_find_answer_rrset(qinfo, msg->rep); 3060 /* If there was no DS rrset, then we have misclassified 3061 * this message. */ 3062 if(!ds) { 3063 log_warn("internal error: POSITIVE DS response was " 3064 "missing DS."); 3065 reason = "no DS record"; 3066 errinf_ede(qstate, reason, reason_bogus); 3067 goto return_bogus; 3068 } 3069 /* Verify only returns BOGUS or SECURE. If the rrset is 3070 * bogus, then we are done. */ 3071 sec = val_verify_rrset_entry(qstate->env, ve, ds, 3072 vq->key_entry, &reason, &reason_bogus, 3073 LDNS_SECTION_ANSWER, qstate, vq, &verified, reasonbuf, 3074 sizeof(reasonbuf)); 3075 if(sec != sec_status_secure) { 3076 verbose(VERB_DETAIL, "DS rrset in DS response did " 3077 "not verify"); 3078 errinf_ede(qstate, reason, reason_bogus); 3079 goto return_bogus; 3080 } 3081 3082 /* If the DS rrset validates, we still have to make sure 3083 * that they are usable. */ 3084 if(!val_dsset_isusable(ds)) { 3085 /* If they aren't usable, then we treat it like 3086 * there was no DS. */ 3087 *ke = key_entry_create_null(qstate->region, 3088 qinfo->qname, qinfo->qname_len, qinfo->qclass, 3089 ub_packed_rrset_ttl(ds), 3090 LDNS_EDE_UNSUPPORTED_DS_DIGEST, NULL, 3091 *qstate->env->now); 3092 return (*ke) == NULL; 3093 } 3094 3095 /* Otherwise, we return the positive response. */ 3096 log_query_info(VERB_DETAIL, "validated DS", qinfo); 3097 *ke = key_entry_create_rrset(qstate->region, 3098 qinfo->qname, qinfo->qname_len, qinfo->qclass, ds, 3099 NULL, LDNS_EDE_NONE, NULL, *qstate->env->now); 3100 return (*ke) == NULL; 3101 } else if(subtype == VAL_CLASS_NODATA || 3102 subtype == VAL_CLASS_NAMEERROR) { 3103 /* NODATA means that the qname exists, but that there was 3104 * no DS. This is a pretty normal case. */ 3105 time_t proof_ttl = 0; 3106 enum sec_status sec; 3107 3108 /* make sure there are NSECs or NSEC3s with signatures */ 3109 if(!val_has_signed_nsecs(msg->rep, &reason)) { 3110 verbose(VERB_ALGO, "no NSECs: %s", reason); 3111 reason_bogus = LDNS_EDE_NSEC_MISSING; 3112 errinf_ede(qstate, reason, reason_bogus); 3113 goto return_bogus; 3114 } 3115 3116 /* For subtype Name Error. 3117 * attempt ANS 2.8.1.0 compatibility where it sets rcode 3118 * to nxdomain, but really this is an Nodata/Noerror response. 3119 * Find and prove the empty nonterminal in that case */ 3120 3121 /* Try to prove absence of the DS with NSEC */ 3122 sec = val_nsec_prove_nodata_dsreply( 3123 qstate->env, ve, qinfo, msg->rep, vq->key_entry, 3124 &proof_ttl, &reason, &reason_bogus, qstate, vq, 3125 reasonbuf, sizeof(reasonbuf)); 3126 switch(sec) { 3127 case sec_status_secure: 3128 verbose(VERB_DETAIL, "NSEC RRset for the " 3129 "referral proved no DS."); 3130 *ke = key_entry_create_null(qstate->region, 3131 qinfo->qname, qinfo->qname_len, 3132 qinfo->qclass, proof_ttl, 3133 LDNS_EDE_NONE, NULL, 3134 *qstate->env->now); 3135 return (*ke) == NULL; 3136 case sec_status_insecure: 3137 verbose(VERB_DETAIL, "NSEC RRset for the " 3138 "referral proved not a delegation point"); 3139 *ke = NULL; 3140 return 0; 3141 case sec_status_bogus: 3142 verbose(VERB_DETAIL, "NSEC RRset for the " 3143 "referral did not prove no DS."); 3144 errinf(qstate, reason); 3145 goto return_bogus; 3146 case sec_status_unchecked: 3147 default: 3148 /* NSEC proof did not work, try next */ 3149 break; 3150 } 3151 3152 if(!nsec3_cache_table_init(&vq->nsec3_cache_table, qstate->region)) { 3153 log_err("malloc failure in ds_response_to_ke for " 3154 "NSEC3 cache"); 3155 reason = "malloc failure"; 3156 errinf_ede(qstate, reason, 0); 3157 goto return_bogus; 3158 } 3159 sec = nsec3_prove_nods(qstate->env, ve, 3160 msg->rep->rrsets + msg->rep->an_numrrsets, 3161 msg->rep->ns_numrrsets, qinfo, vq->key_entry, &reason, 3162 &reason_bogus, qstate, vq, &vq->nsec3_cache_table, 3163 reasonbuf, sizeof(reasonbuf)); 3164 switch(sec) { 3165 case sec_status_insecure: 3166 /* case insecure also continues to unsigned 3167 * space. If nsec3-iter-count too high or 3168 * optout, then treat below as unsigned */ 3169 case sec_status_secure: 3170 verbose(VERB_DETAIL, "NSEC3s for the " 3171 "referral proved no DS."); 3172 *ke = key_entry_create_null(qstate->region, 3173 qinfo->qname, qinfo->qname_len, 3174 qinfo->qclass, proof_ttl, 3175 LDNS_EDE_NONE, NULL, 3176 *qstate->env->now); 3177 return (*ke) == NULL; 3178 case sec_status_indeterminate: 3179 verbose(VERB_DETAIL, "NSEC3s for the " 3180 "referral proved no delegation"); 3181 *ke = NULL; 3182 return 0; 3183 case sec_status_bogus: 3184 verbose(VERB_DETAIL, "NSEC3s for the " 3185 "referral did not prove no DS."); 3186 errinf_ede(qstate, reason, reason_bogus); 3187 goto return_bogus; 3188 case sec_status_unchecked: 3189 return 2; 3190 default: 3191 /* NSEC3 proof did not work */ 3192 break; 3193 } 3194 3195 /* Apparently, no available NSEC/NSEC3 proved NODATA, so 3196 * this is BOGUS. */ 3197 verbose(VERB_DETAIL, "DS %s ran out of options, so return " 3198 "bogus", val_classification_to_string(subtype)); 3199 reason = "no DS but also no proof of that"; 3200 errinf_ede(qstate, reason, reason_bogus); 3201 goto return_bogus; 3202 } else if(subtype == VAL_CLASS_CNAME || 3203 subtype == VAL_CLASS_CNAMENOANSWER) { 3204 /* if the CNAME matches the exact name we want and is signed 3205 * properly, then also, we are sure that no DS exists there, 3206 * much like a NODATA proof */ 3207 enum sec_status sec; 3208 struct ub_packed_rrset_key* cname; 3209 cname = reply_find_rrset_section_an(msg->rep, qinfo->qname, 3210 qinfo->qname_len, LDNS_RR_TYPE_CNAME, qinfo->qclass); 3211 if(!cname) { 3212 reason = "validator classified CNAME but no " 3213 "CNAME of the queried name for DS"; 3214 errinf_ede(qstate, reason, reason_bogus); 3215 goto return_bogus; 3216 } 3217 if(((struct packed_rrset_data*)cname->entry.data)->rrsig_count 3218 == 0) { 3219 if(msg->rep->an_numrrsets != 0 && ntohs(msg->rep-> 3220 rrsets[0]->rk.type)==LDNS_RR_TYPE_DNAME) { 3221 reason = "DS got DNAME answer"; 3222 } else { 3223 reason = "DS got unsigned CNAME answer"; 3224 } 3225 errinf_ede(qstate, reason, reason_bogus); 3226 goto return_bogus; 3227 } 3228 sec = val_verify_rrset_entry(qstate->env, ve, cname, 3229 vq->key_entry, &reason, &reason_bogus, 3230 LDNS_SECTION_ANSWER, qstate, vq, &verified, reasonbuf, 3231 sizeof(reasonbuf)); 3232 if(sec == sec_status_secure) { 3233 /* Check for wildcard expansion */ 3234 uint8_t* wc = NULL; 3235 size_t wl = 0; 3236 3237 if(!val_rrset_wildcard(cname, &wc, &wl)) { 3238 verbose(VERB_ALGO, "CNAME has inconsistent wildcard signatures"); 3239 reason = "wildcard CNAME inconsistent signatures"; 3240 errinf_ede(qstate, reason, reason_bogus); 3241 goto return_bogus; 3242 } 3243 3244 if(wc != NULL) { 3245 /* Wildcard expansion detected - require NSEC proof */ 3246 /* So this is a wildcard CNAME response to DS. 3247 * If the wildcard is bogus then we have bogus. 3248 * If the wildcard is true, then there is 3249 * not a referral point here or lower, 3250 * that can be insecure, 3251 * and also no DS records, here or lower. */ 3252 /* For a valid chain, to DS, but this 3253 * wildcard CNAME happens in a middle label, 3254 * then that can not happen, because there is 3255 * data under that label, and thus the wildcard 3256 * should not expand. 3257 * If we are going to the wildcard, that also 3258 * does not expand the wildcard, when above it. 3259 * So for valids lookup chains to DS, no 3260 * wildcard CNAME is expected on middle labels. 3261 * For lookups to an insecure point, the 3262 * delegation is information under the label, 3263 * and thus the wildcard does not expand. 3264 * So, no insecure point is possible. 3265 * Can not get a valid chain of trust, or 3266 * to a delegation point for insecure. 3267 * Or the wildcard, its nxdomain for the qname 3268 * proof, is invalid, in which case this is 3269 * a bogus reply. 3270 * If this was a lookup where a wildcard 3271 * expansion is genuinely expected, eg, 3272 * a dnssec valid wildcard query, then the 3273 * lookup should go to the right point, and 3274 * not into the wildcard under the zone name. 3275 * For insecure, or wildcard missing 3276 * signatures, it would have to have found 3277 * the DS or insecure point earlier, in the 3278 * downwards search. 3279 * So for missing signatures, it turns the 3280 * missing signatures into a failure to the 3281 * wildcard CNAME, as the reported log. 3282 */ 3283 verbose(VERB_ALGO, "wildcard CNAME in chain of trust means no DS can be found and it is also not a delegation point that can be insecure"); 3284 reason = "wildcard CNAME in chain of trust means no DS found and it is also not a delegation point that can be insecure"; 3285 errinf_ede(qstate, reason, reason_bogus); 3286 goto return_bogus; 3287 } 3288 3289 verbose(VERB_ALGO, "CNAME validated, " 3290 "proof that DS does not exist"); 3291 /* and that it is not a referral point */ 3292 *ke = NULL; 3293 return 0; 3294 } 3295 errinf(qstate, "CNAME in DS response was not secure."); 3296 errinf_ede(qstate, reason, reason_bogus); 3297 goto return_bogus; 3298 } else { 3299 verbose(VERB_QUERY, "Encountered an unhandled type of " 3300 "DS response, thus bogus."); 3301 errinf(qstate, "no DS and"); 3302 reason = "no DS"; 3303 if(FLAGS_GET_RCODE(msg->rep->flags) != LDNS_RCODE_NOERROR) { 3304 char rc[16]; 3305 rc[0]=0; 3306 (void)sldns_wire2str_rcode_buf((int)FLAGS_GET_RCODE( 3307 msg->rep->flags), rc, sizeof(rc)); 3308 errinf(qstate, rc); 3309 } else errinf(qstate, val_classification_to_string(subtype)); 3310 errinf(qstate, "message fails to prove that"); 3311 goto return_bogus; 3312 } 3313 return_bogus: 3314 *ke = key_entry_create_bad(qstate->region, qinfo->qname, 3315 qinfo->qname_len, qinfo->qclass, BOGUS_KEY_TTL, 3316 reason_bogus, reason, *qstate->env->now); 3317 return (*ke) == NULL; 3318 } 3319 3320 /** 3321 * Process DS response. Called from inform_supers. 3322 * Because it is in inform_supers, the mesh itself is busy doing callbacks 3323 * for a state that is to be deleted soon; don't touch the mesh; instead 3324 * set a state in the super, as the super will be reactivated soon. 3325 * Perform processing to determine what state to set in the super. 3326 * 3327 * @param qstate: query state that is validating and asked for a DS. 3328 * @param vq: validator query state 3329 * @param id: module id. 3330 * @param rcode: rcode result value. 3331 * @param msg: result message (if rcode is OK). 3332 * @param qinfo: from the sub query state, query info. 3333 * @param origin: the origin of msg. 3334 * @param suspend: returned true if the task takes too long and needs to 3335 * suspend to continue the effort later. 3336 * @param sub_qstate: the sub query state, that is the lookup that fetched 3337 * the trust anchor data, it contains error information for the answer. 3338 * Can be NULL. 3339 */ 3340 static void 3341 process_ds_response(struct module_qstate* qstate, struct val_qstate* vq, 3342 int id, int rcode, struct dns_msg* msg, struct query_info* qinfo, 3343 struct sock_list* origin, int* suspend, 3344 struct module_qstate* sub_qstate) 3345 { 3346 struct val_env* ve = (struct val_env*)qstate->env->modinfo[id]; 3347 struct key_entry_key* dske = NULL; 3348 uint8_t* olds = vq->empty_DS_name; 3349 int ret; 3350 *suspend = 0; 3351 vq->num_nsec_attempts = 0; 3352 vq->empty_DS_name = NULL; 3353 if(sub_qstate && sub_qstate->rpz_applied) { 3354 verbose(VERB_ALGO, "rpz was applied to the DS lookup, " 3355 "make it insecure"); 3356 vq->key_entry = NULL; 3357 vq->state = VAL_FINISHED_STATE; 3358 vq->chase_reply->security = sec_status_insecure; 3359 return; 3360 } 3361 ret = ds_response_to_ke(qstate, vq, id, rcode, msg, qinfo, &dske, 3362 sub_qstate); 3363 /* New NSEC attempt count for next message validation. */ 3364 vq->num_nsec_attempts = 0; 3365 if(ret != 0) { 3366 switch(ret) { 3367 case 1: 3368 log_err("malloc failure in process_ds_response"); 3369 vq->key_entry = NULL; /* make it error */ 3370 vq->state = VAL_VALIDATE_STATE; 3371 return; 3372 case 2: 3373 *suspend = 1; 3374 return; 3375 default: 3376 log_err("unhandled error value for ds_response_to_ke"); 3377 vq->key_entry = NULL; /* make it error */ 3378 vq->state = VAL_VALIDATE_STATE; 3379 return; 3380 } 3381 } 3382 if(dske == NULL) { 3383 vq->empty_DS_name = regional_alloc_init(qstate->region, 3384 qinfo->qname, qinfo->qname_len); 3385 if(!vq->empty_DS_name) { 3386 log_err("malloc failure in empty_DS_name"); 3387 vq->key_entry = NULL; /* make it error */ 3388 vq->state = VAL_VALIDATE_STATE; 3389 return; 3390 } 3391 vq->empty_DS_len = qinfo->qname_len; 3392 vq->chain_blacklist = NULL; 3393 /* ds response indicated that we aren't on a delegation point. 3394 * Keep the forState.state on FINDKEY. */ 3395 } else if(key_entry_isgood(dske)) { 3396 vq->ds_rrset = key_entry_get_rrset(dske, qstate->region); 3397 if(!vq->ds_rrset) { 3398 log_err("malloc failure in process DS"); 3399 vq->key_entry = NULL; /* make it error */ 3400 vq->state = VAL_VALIDATE_STATE; 3401 return; 3402 } 3403 vq->chain_blacklist = NULL; /* fresh blacklist for next part*/ 3404 /* Keep the forState.state on FINDKEY. */ 3405 } else if(key_entry_isbad(dske) 3406 && val_can_restart(qstate, vq, ve)) { 3407 vq->empty_DS_name = olds; 3408 val_blacklist(&vq->chain_blacklist, qstate->region, origin, 1); 3409 qstate->errinf = NULL; 3410 vq->restart_count++; 3411 } else { 3412 if(key_entry_isbad(dske)) { 3413 errinf_origin(qstate, origin); 3414 errinf_dname(qstate, "for DS", qinfo->qname); 3415 } 3416 /* NOTE: the reason for the DS to be not good (that is, 3417 * either bad or null) should have been logged by 3418 * dsResponseToKE. */ 3419 vq->key_entry = dske; 3420 /* The FINDKEY phase has ended, so move on. */ 3421 vq->state = VAL_VALIDATE_STATE; 3422 } 3423 } 3424 3425 /** 3426 * Process DNSKEY response. Called from inform_supers. 3427 * Sets the key entry in the state. 3428 * Because it is in inform_supers, the mesh itself is busy doing callbacks 3429 * for a state that is to be deleted soon; don't touch the mesh; instead 3430 * set a state in the super, as the super will be reactivated soon. 3431 * Perform processing to determine what state to set in the super. 3432 * 3433 * @param qstate: query state that is validating and asked for a DNSKEY. 3434 * @param vq: validator query state 3435 * @param id: module id. 3436 * @param rcode: rcode result value. 3437 * @param msg: result message (if rcode is OK). 3438 * @param qinfo: from the sub query state, query info. 3439 * @param origin: the origin of msg. 3440 * @param sub_qstate: the sub query state, that is the lookup that fetched 3441 * the trust anchor data, it contains error information for the answer. 3442 */ 3443 static void 3444 process_dnskey_response(struct module_qstate* qstate, struct val_qstate* vq, 3445 int id, int rcode, struct dns_msg* msg, struct query_info* qinfo, 3446 struct sock_list* origin, struct module_qstate* sub_qstate) 3447 { 3448 struct val_env* ve = (struct val_env*)qstate->env->modinfo[id]; 3449 struct key_entry_key* old = vq->key_entry; 3450 struct ub_packed_rrset_key* dnskey = NULL; 3451 int downprot; 3452 char reasonbuf[256]; 3453 char* reason = NULL; 3454 sldns_ede_code reason_bogus = LDNS_EDE_DNSSEC_BOGUS; 3455 3456 vq->num_nsec_attempts = 0; 3457 if(sub_qstate && sub_qstate->rpz_applied) { 3458 verbose(VERB_ALGO, "rpz was applied to the DNSKEY lookup, " 3459 "make it insecure"); 3460 vq->key_entry = NULL; 3461 vq->state = VAL_FINISHED_STATE; 3462 vq->chase_reply->security = sec_status_insecure; 3463 return; 3464 } 3465 3466 if(rcode == LDNS_RCODE_NOERROR) 3467 dnskey = reply_find_answer_rrset(qinfo, msg->rep); 3468 3469 if(dnskey == NULL) { 3470 char* err; 3471 char rstr[1024]; 3472 /* bad response */ 3473 verbose(VERB_DETAIL, "Missing DNSKEY RRset in response to " 3474 "DNSKEY query."); 3475 3476 if(val_can_restart(qstate, vq, ve)) { 3477 val_blacklist(&vq->chain_blacklist, qstate->region, 3478 origin, 1); 3479 qstate->errinf = NULL; 3480 vq->restart_count++; 3481 return; 3482 } 3483 err = errinf_to_str_misc(sub_qstate); 3484 if(!err) { 3485 snprintf(rstr, sizeof(rstr), "No DNSKEY record"); 3486 } else { 3487 snprintf(rstr, sizeof(rstr), "No DNSKEY record " 3488 "[%s]", err); 3489 } 3490 reason_bogus = LDNS_EDE_DNSKEY_MISSING; 3491 vq->key_entry = key_entry_create_bad(qstate->region, 3492 qinfo->qname, qinfo->qname_len, qinfo->qclass, 3493 BOGUS_KEY_TTL, reason_bogus, rstr, *qstate->env->now); 3494 if(!vq->key_entry) { 3495 log_err("alloc failure in missing dnskey response"); 3496 /* key_entry is NULL for failure in Validate */ 3497 } 3498 errinf_ede(qstate, rstr, reason_bogus); 3499 errinf_origin(qstate, origin); 3500 errinf_dname(qstate, "for key", qinfo->qname); 3501 vq->state = VAL_VALIDATE_STATE; 3502 return; 3503 } 3504 if(!vq->ds_rrset) { 3505 log_err("internal error: no DS rrset for new DNSKEY response"); 3506 vq->key_entry = NULL; 3507 vq->state = VAL_VALIDATE_STATE; 3508 return; 3509 } 3510 downprot = qstate->env->cfg->harden_algo_downgrade; 3511 vq->key_entry = val_verify_new_DNSKEYs(qstate->region, qstate->env, 3512 ve, dnskey, vq->ds_rrset, downprot, &reason, &reason_bogus, 3513 qstate, vq, reasonbuf, sizeof(reasonbuf)); 3514 /* New NSEC attempt count for next message validation. */ 3515 vq->num_nsec_attempts = 0; 3516 3517 if(!vq->key_entry) { 3518 log_err("out of memory in verify new DNSKEYs"); 3519 vq->state = VAL_VALIDATE_STATE; 3520 return; 3521 } 3522 /* If the key entry isBad or isNull, then we can move on to the next 3523 * state. */ 3524 if(!key_entry_isgood(vq->key_entry)) { 3525 if(key_entry_isbad(vq->key_entry)) { 3526 if(val_can_restart(qstate, vq, ve)) { 3527 val_blacklist(&vq->chain_blacklist, 3528 qstate->region, origin, 1); 3529 qstate->errinf = NULL; 3530 vq->restart_count++; 3531 vq->key_entry = old; 3532 return; 3533 } 3534 verbose(VERB_DETAIL, "Did not match a DS to a DNSKEY, " 3535 "thus bogus."); 3536 errinf_ede(qstate, reason, reason_bogus); 3537 errinf_origin(qstate, origin); 3538 errinf_dname(qstate, "for key", qinfo->qname); 3539 } 3540 vq->chain_blacklist = NULL; 3541 vq->state = VAL_VALIDATE_STATE; 3542 return; 3543 } 3544 vq->chain_blacklist = NULL; 3545 qstate->errinf = NULL; 3546 3547 /* The DNSKEY validated, so cache it as a trusted key rrset. */ 3548 key_cache_insert(ve->kcache, vq->key_entry, 3549 qstate->env->cfg->val_log_level >= 2); 3550 3551 /* If good, we stay in the FINDKEY state. */ 3552 log_query_info(VERB_DETAIL, "validated DNSKEY", qinfo); 3553 } 3554 3555 /** 3556 * Process prime response 3557 * Sets the key entry in the state. 3558 * 3559 * @param qstate: query state that is validating and primed a trust anchor. 3560 * @param vq: validator query state 3561 * @param id: module id. 3562 * @param rcode: rcode result value. 3563 * @param msg: result message (if rcode is OK). 3564 * @param origin: the origin of msg. 3565 * @param sub_qstate: the sub query state, that is the lookup that fetched 3566 * the trust anchor data, it contains error information for the answer. 3567 */ 3568 static void 3569 process_prime_response(struct module_qstate* qstate, struct val_qstate* vq, 3570 int id, int rcode, struct dns_msg* msg, struct sock_list* origin, 3571 struct module_qstate* sub_qstate) 3572 { 3573 struct val_env* ve = (struct val_env*)qstate->env->modinfo[id]; 3574 struct ub_packed_rrset_key* dnskey_rrset = NULL; 3575 struct trust_anchor* ta = anchor_find(qstate->env->anchors, 3576 vq->trust_anchor_name, vq->trust_anchor_labs, 3577 vq->trust_anchor_len, vq->qchase.qclass); 3578 vq->num_nsec_attempts = 0; 3579 if(!ta) { 3580 /* trust anchor revoked, restart with less anchors */ 3581 vq->state = VAL_INIT_STATE; 3582 if(!vq->trust_anchor_name) 3583 vq->state = VAL_VALIDATE_STATE; /* break a loop */ 3584 vq->trust_anchor_name = NULL; 3585 return; 3586 } 3587 /* Fetch and validate the keyEntry that corresponds to the 3588 * current trust anchor. */ 3589 if(rcode == LDNS_RCODE_NOERROR) { 3590 dnskey_rrset = reply_find_rrset_section_an(msg->rep, 3591 ta->name, ta->namelen, LDNS_RR_TYPE_DNSKEY, 3592 ta->dclass); 3593 } 3594 3595 if(ta->autr) { 3596 if(!autr_process_prime(qstate->env, ve, ta, dnskey_rrset, 3597 qstate, vq)) { 3598 /* New NSEC attempt count for next message validation. */ 3599 vq->num_nsec_attempts = 0; 3600 /* trust anchor revoked, restart with less anchors */ 3601 vq->state = VAL_INIT_STATE; 3602 vq->trust_anchor_name = NULL; 3603 return; 3604 } 3605 } 3606 vq->key_entry = primeResponseToKE(dnskey_rrset, ta, qstate, vq, id, 3607 sub_qstate); 3608 lock_basic_unlock(&ta->lock); 3609 /* New NSEC attempt count for next message validation. */ 3610 vq->num_nsec_attempts = 0; 3611 if(vq->key_entry) { 3612 if(key_entry_isbad(vq->key_entry) 3613 && val_can_restart(qstate, vq, ve)) { 3614 val_blacklist(&vq->chain_blacklist, qstate->region, 3615 origin, 1); 3616 qstate->errinf = NULL; 3617 vq->restart_count++; 3618 vq->key_entry = NULL; 3619 vq->state = VAL_INIT_STATE; 3620 return; 3621 } 3622 vq->chain_blacklist = NULL; 3623 errinf_origin(qstate, origin); 3624 errinf_dname(qstate, "for trust anchor", ta->name); 3625 /* store the freshly primed entry in the cache */ 3626 key_cache_insert(ve->kcache, vq->key_entry, 3627 qstate->env->cfg->val_log_level >= 2); 3628 } 3629 3630 /* If the result of the prime is a null key, skip the FINDKEY state.*/ 3631 if(!vq->key_entry || key_entry_isnull(vq->key_entry) || 3632 key_entry_isbad(vq->key_entry)) { 3633 vq->state = VAL_VALIDATE_STATE; 3634 } 3635 /* the qstate will be reactivated after inform_super is done */ 3636 } 3637 3638 /* 3639 * inform validator super. 3640 * 3641 * @param qstate: query state that finished. 3642 * @param id: module id. 3643 * @param super: the qstate to inform. 3644 */ 3645 void 3646 val_inform_super(struct module_qstate* qstate, int id, 3647 struct module_qstate* super) 3648 { 3649 struct val_qstate* vq = (struct val_qstate*)super->minfo[id]; 3650 log_query_info(VERB_ALGO, "validator: inform_super, sub is", 3651 &qstate->qinfo); 3652 log_query_info(VERB_ALGO, "super is", &super->qinfo); 3653 if(!vq) { 3654 verbose(VERB_ALGO, "super: has no validator state"); 3655 return; 3656 } 3657 /* Pick up the global quota limit from the subquery. */ 3658 if(qstate->global_quota_reached > qstate->global_quota_started) { 3659 super->global_quota_reached += qstate->global_quota_reached - 3660 qstate->global_quota_started; 3661 } 3662 if(vq->wait_prime_ta) { 3663 vq->wait_prime_ta = 0; 3664 process_prime_response(super, vq, id, qstate->return_rcode, 3665 qstate->return_msg, qstate->reply_origin, qstate); 3666 return; 3667 } 3668 if(qstate->qinfo.qtype == LDNS_RR_TYPE_DS) { 3669 int suspend; 3670 process_ds_response(super, vq, id, qstate->return_rcode, 3671 qstate->return_msg, &qstate->qinfo, 3672 qstate->reply_origin, &suspend, qstate); 3673 /* If NSEC3 was needed during validation, NULL the NSEC3 cache; 3674 * it will be re-initiated if needed later on. 3675 * Validation (and the cache table) are happening/allocated in 3676 * the super qstate whilst the RRs are allocated (and pointed 3677 * to) in this sub qstate. */ 3678 if(vq->nsec3_cache_table.ct) { 3679 vq->nsec3_cache_table.ct = NULL; 3680 } 3681 if(suspend) { 3682 /* deep copy the return_msg to vq->sub_ds_msg; it will 3683 * be resumed later in the super state with the caveat 3684 * that the initial calculations will be re-calculated 3685 * and re-suspended there before continuing. */ 3686 vq->sub_ds_msg = dns_msg_deepcopy_region( 3687 qstate->return_msg, super->region); 3688 } 3689 return; 3690 } else if(qstate->qinfo.qtype == LDNS_RR_TYPE_DNSKEY) { 3691 process_dnskey_response(super, vq, id, qstate->return_rcode, 3692 qstate->return_msg, &qstate->qinfo, 3693 qstate->reply_origin, qstate); 3694 return; 3695 } 3696 log_err("internal error in validator: no inform_supers possible"); 3697 } 3698 3699 void 3700 val_clear(struct module_qstate* qstate, int id) 3701 { 3702 struct val_qstate* vq; 3703 if(!qstate) 3704 return; 3705 vq = (struct val_qstate*)qstate->minfo[id]; 3706 if(vq) { 3707 if(vq->suspend_timer) { 3708 comm_timer_delete(vq->suspend_timer); 3709 } 3710 } 3711 /* everything is allocated in the region, so assign NULL */ 3712 qstate->minfo[id] = NULL; 3713 } 3714 3715 size_t 3716 val_get_mem(struct module_env* env, int id) 3717 { 3718 struct val_env* ve = (struct val_env*)env->modinfo[id]; 3719 if(!ve) 3720 return 0; 3721 return sizeof(*ve) + key_cache_get_mem(ve->kcache) + 3722 val_neg_get_mem(ve->neg_cache) + 3723 sizeof(size_t)*2*ve->nsec3_keyiter_count; 3724 } 3725 3726 /** 3727 * The validator function block 3728 */ 3729 static struct module_func_block val_block = { 3730 "validator", 3731 NULL, NULL, &val_init, &val_deinit, &val_operate, &val_inform_super, 3732 &val_clear, &val_get_mem 3733 }; 3734 3735 struct module_func_block* 3736 val_get_funcblock(void) 3737 { 3738 return &val_block; 3739 } 3740 3741 const char* 3742 val_state_to_string(enum val_state state) 3743 { 3744 switch(state) { 3745 case VAL_INIT_STATE: return "VAL_INIT_STATE"; 3746 case VAL_FINDKEY_STATE: return "VAL_FINDKEY_STATE"; 3747 case VAL_VALIDATE_STATE: return "VAL_VALIDATE_STATE"; 3748 case VAL_FINISHED_STATE: return "VAL_FINISHED_STATE"; 3749 } 3750 return "UNKNOWN VALIDATOR STATE"; 3751 } 3752 3753