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