1933707f3Ssthen /* 2933707f3Ssthen * validator/val_utils.c - validator utility functions. 3933707f3Ssthen * 4933707f3Ssthen * Copyright (c) 2007, NLnet Labs. All rights reserved. 5933707f3Ssthen * 6933707f3Ssthen * This software is open source. 7933707f3Ssthen * 8933707f3Ssthen * Redistribution and use in source and binary forms, with or without 9933707f3Ssthen * modification, are permitted provided that the following conditions 10933707f3Ssthen * are met: 11933707f3Ssthen * 12933707f3Ssthen * Redistributions of source code must retain the above copyright notice, 13933707f3Ssthen * this list of conditions and the following disclaimer. 14933707f3Ssthen * 15933707f3Ssthen * Redistributions in binary form must reproduce the above copyright notice, 16933707f3Ssthen * this list of conditions and the following disclaimer in the documentation 17933707f3Ssthen * and/or other materials provided with the distribution. 18933707f3Ssthen * 19933707f3Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may 20933707f3Ssthen * be used to endorse or promote products derived from this software without 21933707f3Ssthen * specific prior written permission. 22933707f3Ssthen * 23933707f3Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 245d76a658Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 255d76a658Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 265d76a658Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 275d76a658Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 285d76a658Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 295d76a658Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 305d76a658Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 315d76a658Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 325d76a658Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 335d76a658Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34933707f3Ssthen */ 35933707f3Ssthen 36933707f3Ssthen /** 37933707f3Ssthen * \file 38933707f3Ssthen * 39933707f3Ssthen * This file contains helper functions for the validator module. 40933707f3Ssthen */ 41933707f3Ssthen #include "config.h" 42933707f3Ssthen #include "validator/val_utils.h" 43933707f3Ssthen #include "validator/validator.h" 44933707f3Ssthen #include "validator/val_kentry.h" 45933707f3Ssthen #include "validator/val_sigcrypt.h" 46933707f3Ssthen #include "validator/val_anchor.h" 47933707f3Ssthen #include "validator/val_nsec.h" 48933707f3Ssthen #include "validator/val_neg.h" 49933707f3Ssthen #include "services/cache/rrset.h" 50933707f3Ssthen #include "services/cache/dns.h" 51933707f3Ssthen #include "util/data/msgreply.h" 52933707f3Ssthen #include "util/data/packed_rrset.h" 53933707f3Ssthen #include "util/data/dname.h" 54933707f3Ssthen #include "util/net_help.h" 55933707f3Ssthen #include "util/module.h" 56933707f3Ssthen #include "util/regional.h" 572be9e038Ssthen #include "util/config_file.h" 5832e31f52Ssthen #include "sldns/wire2str.h" 5932e31f52Ssthen #include "sldns/parseutil.h" 60933707f3Ssthen 61817bdb8fSflorian /** Maximum allowed digest match failures per DS, for DNSKEYs with the same 62817bdb8fSflorian * properties */ 63817bdb8fSflorian #define MAX_DS_MATCH_FAILURES 4 64817bdb8fSflorian 65933707f3Ssthen enum val_classification 66933707f3Ssthen val_classify_response(uint16_t query_flags, struct query_info* origqinf, 67933707f3Ssthen struct query_info* qinf, struct reply_info* rep, size_t skip) 68933707f3Ssthen { 69933707f3Ssthen int rcode = (int)FLAGS_GET_RCODE(rep->flags); 70933707f3Ssthen size_t i; 71933707f3Ssthen 72933707f3Ssthen /* Normal Name Error's are easy to detect -- but don't mistake a CNAME 73933707f3Ssthen * chain ending in NXDOMAIN. */ 74933707f3Ssthen if(rcode == LDNS_RCODE_NXDOMAIN && rep->an_numrrsets == 0) 75933707f3Ssthen return VAL_CLASS_NAMEERROR; 76933707f3Ssthen 77933707f3Ssthen /* check for referral: nonRD query and it looks like a nodata */ 78933707f3Ssthen if(!(query_flags&BIT_RD) && rep->an_numrrsets == 0 && 79933707f3Ssthen rcode == LDNS_RCODE_NOERROR) { 80933707f3Ssthen /* SOA record in auth indicates it is NODATA instead. 81933707f3Ssthen * All validation requiring NODATA messages have SOA in 82933707f3Ssthen * authority section. */ 83933707f3Ssthen /* uses fact that answer section is empty */ 84933707f3Ssthen int saw_ns = 0; 85933707f3Ssthen for(i=0; i<rep->ns_numrrsets; i++) { 86933707f3Ssthen if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_SOA) 87933707f3Ssthen return VAL_CLASS_NODATA; 88933707f3Ssthen if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_DS) 89933707f3Ssthen return VAL_CLASS_REFERRAL; 90933707f3Ssthen if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NS) 91933707f3Ssthen saw_ns = 1; 92933707f3Ssthen } 93933707f3Ssthen return saw_ns?VAL_CLASS_REFERRAL:VAL_CLASS_NODATA; 94933707f3Ssthen } 95933707f3Ssthen /* root referral where NS set is in the answer section */ 96933707f3Ssthen if(!(query_flags&BIT_RD) && rep->ns_numrrsets == 0 && 97933707f3Ssthen rep->an_numrrsets == 1 && rcode == LDNS_RCODE_NOERROR && 98933707f3Ssthen ntohs(rep->rrsets[0]->rk.type) == LDNS_RR_TYPE_NS && 99933707f3Ssthen query_dname_compare(rep->rrsets[0]->rk.dname, 100933707f3Ssthen origqinf->qname) != 0) 101933707f3Ssthen return VAL_CLASS_REFERRAL; 102933707f3Ssthen 103933707f3Ssthen /* dump bad messages */ 104933707f3Ssthen if(rcode != LDNS_RCODE_NOERROR && rcode != LDNS_RCODE_NXDOMAIN) 105933707f3Ssthen return VAL_CLASS_UNKNOWN; 106933707f3Ssthen /* next check if the skip into the answer section shows no answer */ 107933707f3Ssthen if(skip>0 && rep->an_numrrsets <= skip) 108933707f3Ssthen return VAL_CLASS_CNAMENOANSWER; 109933707f3Ssthen 110933707f3Ssthen /* Next is NODATA */ 111933707f3Ssthen if(rcode == LDNS_RCODE_NOERROR && rep->an_numrrsets == 0) 112933707f3Ssthen return VAL_CLASS_NODATA; 113933707f3Ssthen 114933707f3Ssthen /* We distinguish between CNAME response and other positive/negative 115933707f3Ssthen * responses because CNAME answers require extra processing. */ 116933707f3Ssthen 117933707f3Ssthen /* We distinguish between ANY and CNAME or POSITIVE because 118933707f3Ssthen * ANY responses are validated differently. */ 119933707f3Ssthen if(rcode == LDNS_RCODE_NOERROR && qinf->qtype == LDNS_RR_TYPE_ANY) 120933707f3Ssthen return VAL_CLASS_ANY; 121933707f3Ssthen 1222bdc0ed1Ssthen /* For the query type DNAME, the name matters. Equal name is the 1232bdc0ed1Ssthen * answer looked for, but a subdomain redirects the query. */ 1242bdc0ed1Ssthen if(qinf->qtype == LDNS_RR_TYPE_DNAME) { 1252bdc0ed1Ssthen for(i=skip; i<rep->an_numrrsets; i++) { 1262bdc0ed1Ssthen if(rcode == LDNS_RCODE_NOERROR && 1272bdc0ed1Ssthen ntohs(rep->rrsets[i]->rk.type) 1282bdc0ed1Ssthen == LDNS_RR_TYPE_DNAME && 1292bdc0ed1Ssthen query_dname_compare(qinf->qname, 1302bdc0ed1Ssthen rep->rrsets[i]->rk.dname) == 0) { 1312bdc0ed1Ssthen /* type is DNAME and name is equal, it is 1322bdc0ed1Ssthen * the answer. For the query name a subdomain 1332bdc0ed1Ssthen * of the rrset.dname it would redirect. */ 1342bdc0ed1Ssthen return VAL_CLASS_POSITIVE; 1352bdc0ed1Ssthen } 1362bdc0ed1Ssthen if(ntohs(rep->rrsets[i]->rk.type) 1372bdc0ed1Ssthen == LDNS_RR_TYPE_CNAME) 1382bdc0ed1Ssthen return VAL_CLASS_CNAME; 1392bdc0ed1Ssthen } 1402bdc0ed1Ssthen log_dns_msg("validator: error. failed to classify response message: ", 1412bdc0ed1Ssthen qinf, rep); 1422bdc0ed1Ssthen return VAL_CLASS_UNKNOWN; 1432bdc0ed1Ssthen } 1442bdc0ed1Ssthen 145933707f3Ssthen /* Note that DNAMEs will be ignored here, unless qtype=DNAME. Unless 146933707f3Ssthen * qtype=CNAME, this will yield a CNAME response. */ 147933707f3Ssthen for(i=skip; i<rep->an_numrrsets; i++) { 148933707f3Ssthen if(rcode == LDNS_RCODE_NOERROR && 149933707f3Ssthen ntohs(rep->rrsets[i]->rk.type) == qinf->qtype) 150933707f3Ssthen return VAL_CLASS_POSITIVE; 151933707f3Ssthen if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_CNAME) 152933707f3Ssthen return VAL_CLASS_CNAME; 153933707f3Ssthen } 154933707f3Ssthen log_dns_msg("validator: error. failed to classify response message: ", 155933707f3Ssthen qinf, rep); 156933707f3Ssthen return VAL_CLASS_UNKNOWN; 157933707f3Ssthen } 158933707f3Ssthen 159933707f3Ssthen /** Get signer name from RRSIG */ 160933707f3Ssthen static void 161933707f3Ssthen rrsig_get_signer(uint8_t* data, size_t len, uint8_t** sname, size_t* slen) 162933707f3Ssthen { 163933707f3Ssthen /* RRSIG rdata is not allowed to be compressed, it is stored 164933707f3Ssthen * uncompressed in memory as well, so return a ptr to the name */ 165933707f3Ssthen if(len < 21) { 166933707f3Ssthen /* too short RRSig: 167933707f3Ssthen * short, byte, byte, long, long, long, short, "." is 168933707f3Ssthen * 2 1 1 4 4 4 2 1 = 19 169933707f3Ssthen * and a skip of 18 bytes to the name. 170933707f3Ssthen * +2 for the rdatalen is 21 bytes len for root label */ 171933707f3Ssthen *sname = NULL; 172933707f3Ssthen *slen = 0; 173933707f3Ssthen return; 174933707f3Ssthen } 175933707f3Ssthen data += 20; /* skip the fixed size bits */ 176933707f3Ssthen len -= 20; 177933707f3Ssthen *slen = dname_valid(data, len); 178933707f3Ssthen if(!*slen) { 179933707f3Ssthen /* bad dname in this rrsig. */ 180933707f3Ssthen *sname = NULL; 181933707f3Ssthen return; 182933707f3Ssthen } 183933707f3Ssthen *sname = data; 184933707f3Ssthen } 185933707f3Ssthen 186933707f3Ssthen void 187933707f3Ssthen val_find_rrset_signer(struct ub_packed_rrset_key* rrset, uint8_t** sname, 188933707f3Ssthen size_t* slen) 189933707f3Ssthen { 190933707f3Ssthen struct packed_rrset_data* d = (struct packed_rrset_data*) 191933707f3Ssthen rrset->entry.data; 192933707f3Ssthen /* return signer for first signature, or NULL */ 193933707f3Ssthen if(d->rrsig_count == 0) { 194933707f3Ssthen *sname = NULL; 195933707f3Ssthen *slen = 0; 196933707f3Ssthen return; 197933707f3Ssthen } 198933707f3Ssthen /* get rrsig signer name out of the signature */ 199933707f3Ssthen rrsig_get_signer(d->rr_data[d->count], d->rr_len[d->count], 200933707f3Ssthen sname, slen); 201933707f3Ssthen } 202933707f3Ssthen 203933707f3Ssthen /** 204933707f3Ssthen * Find best signer name in this set of rrsigs. 205933707f3Ssthen * @param rrset: which rrsigs to look through. 206933707f3Ssthen * @param qinf: the query name that needs validation. 207933707f3Ssthen * @param signer_name: the best signer_name. Updated if a better one is found. 208933707f3Ssthen * @param signer_len: length of signer name. 209933707f3Ssthen * @param matchcount: count of current best name (starts at 0 for no match). 210933707f3Ssthen * Updated if match is improved. 211933707f3Ssthen */ 212933707f3Ssthen static void 213933707f3Ssthen val_find_best_signer(struct ub_packed_rrset_key* rrset, 214933707f3Ssthen struct query_info* qinf, uint8_t** signer_name, size_t* signer_len, 215933707f3Ssthen int* matchcount) 216933707f3Ssthen { 217933707f3Ssthen struct packed_rrset_data* d = (struct packed_rrset_data*) 218933707f3Ssthen rrset->entry.data; 219933707f3Ssthen uint8_t* sign; 220933707f3Ssthen size_t i; 221933707f3Ssthen int m; 222933707f3Ssthen for(i=d->count; i<d->count+d->rrsig_count; i++) { 223933707f3Ssthen sign = d->rr_data[i]+2+18; 224933707f3Ssthen /* look at signatures that are valid (long enough), 225933707f3Ssthen * and have a signer name that is a superdomain of qname, 226933707f3Ssthen * and then check the number of labels in the shared topdomain 227933707f3Ssthen * improve the match if possible */ 228933707f3Ssthen if(d->rr_len[i] > 2+19 && /* rdata, sig + root label*/ 229933707f3Ssthen dname_subdomain_c(qinf->qname, sign)) { 230933707f3Ssthen (void)dname_lab_cmp(qinf->qname, 231933707f3Ssthen dname_count_labels(qinf->qname), 232933707f3Ssthen sign, dname_count_labels(sign), &m); 233933707f3Ssthen if(m > *matchcount) { 234933707f3Ssthen *matchcount = m; 235933707f3Ssthen *signer_name = sign; 236933707f3Ssthen (void)dname_count_size_labels(*signer_name, 237933707f3Ssthen signer_len); 238933707f3Ssthen } 239933707f3Ssthen } 240933707f3Ssthen } 241933707f3Ssthen } 242933707f3Ssthen 243*98bc733bSsthen /** Detect if the, unsigned, CNAME is under a previous DNAME RR in the 244*98bc733bSsthen * message, and thus it was generated from that previous DNAME. 245*98bc733bSsthen */ 246*98bc733bSsthen static int 247*98bc733bSsthen cname_under_previous_dname(struct reply_info* rep, size_t cname_idx, 248*98bc733bSsthen size_t* ret) 249*98bc733bSsthen { 250*98bc733bSsthen size_t i; 251*98bc733bSsthen for(i=0; i<cname_idx; i++) { 252*98bc733bSsthen if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_DNAME && 253*98bc733bSsthen dname_strict_subdomain_c(rep->rrsets[cname_idx]-> 254*98bc733bSsthen rk.dname, rep->rrsets[i]->rk.dname)) { 255*98bc733bSsthen *ret = i; 256*98bc733bSsthen return 1; 257*98bc733bSsthen } 258*98bc733bSsthen } 259*98bc733bSsthen *ret = 0; 260*98bc733bSsthen return 0; 261*98bc733bSsthen } 262*98bc733bSsthen 263933707f3Ssthen void 264933707f3Ssthen val_find_signer(enum val_classification subtype, struct query_info* qinf, 265933707f3Ssthen struct reply_info* rep, size_t skip, uint8_t** signer_name, 266933707f3Ssthen size_t* signer_len) 267933707f3Ssthen { 268933707f3Ssthen size_t i; 269933707f3Ssthen 27077079be7Ssthen if(subtype == VAL_CLASS_POSITIVE) { 271933707f3Ssthen /* check for the answer rrset */ 272933707f3Ssthen for(i=skip; i<rep->an_numrrsets; i++) { 273933707f3Ssthen if(query_dname_compare(qinf->qname, 274933707f3Ssthen rep->rrsets[i]->rk.dname) == 0) { 275933707f3Ssthen val_find_rrset_signer(rep->rrsets[i], 276933707f3Ssthen signer_name, signer_len); 2772bdc0ed1Ssthen /* If there was no signer, and the query 2782bdc0ed1Ssthen * was for type CNAME, and this is a CNAME, 2792bdc0ed1Ssthen * and the previous is a DNAME, then this 2802bdc0ed1Ssthen * is the synthesized CNAME, use the signer 2812bdc0ed1Ssthen * of the DNAME record. */ 2822bdc0ed1Ssthen if(*signer_name == NULL && 2832bdc0ed1Ssthen qinf->qtype == LDNS_RR_TYPE_CNAME && 2842bdc0ed1Ssthen ntohs(rep->rrsets[i]->rk.type) == 2852bdc0ed1Ssthen LDNS_RR_TYPE_CNAME && i > skip && 2862bdc0ed1Ssthen ntohs(rep->rrsets[i-1]->rk.type) == 2872bdc0ed1Ssthen LDNS_RR_TYPE_DNAME && 2882bdc0ed1Ssthen dname_strict_subdomain_c(rep->rrsets[i]->rk.dname, rep->rrsets[i-1]->rk.dname)) { 2892bdc0ed1Ssthen val_find_rrset_signer(rep->rrsets[i-1], 2902bdc0ed1Ssthen signer_name, signer_len); 2912bdc0ed1Ssthen } 292933707f3Ssthen return; 293933707f3Ssthen } 294933707f3Ssthen } 295933707f3Ssthen *signer_name = NULL; 296933707f3Ssthen *signer_len = 0; 297933707f3Ssthen } else if(subtype == VAL_CLASS_CNAME) { 298*98bc733bSsthen size_t j; 299933707f3Ssthen /* check for the first signed cname/dname rrset */ 300933707f3Ssthen for(i=skip; i<rep->an_numrrsets; i++) { 301933707f3Ssthen val_find_rrset_signer(rep->rrsets[i], 302933707f3Ssthen signer_name, signer_len); 303933707f3Ssthen if(*signer_name) 304933707f3Ssthen return; 305*98bc733bSsthen if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_CNAME 306*98bc733bSsthen && cname_under_previous_dname(rep, i, &j)) { 307*98bc733bSsthen val_find_rrset_signer(rep->rrsets[j], 308*98bc733bSsthen signer_name, signer_len); 309*98bc733bSsthen return; 310*98bc733bSsthen } 311933707f3Ssthen if(ntohs(rep->rrsets[i]->rk.type) != LDNS_RR_TYPE_DNAME) 312933707f3Ssthen break; /* only check CNAME after a DNAME */ 313933707f3Ssthen } 314933707f3Ssthen *signer_name = NULL; 315933707f3Ssthen *signer_len = 0; 316933707f3Ssthen } else if(subtype == VAL_CLASS_NAMEERROR 317933707f3Ssthen || subtype == VAL_CLASS_NODATA) { 318933707f3Ssthen /*Check to see if the AUTH section NSEC record(s) have rrsigs*/ 319933707f3Ssthen for(i=rep->an_numrrsets; i< 320933707f3Ssthen rep->an_numrrsets+rep->ns_numrrsets; i++) { 321933707f3Ssthen if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NSEC 322933707f3Ssthen || ntohs(rep->rrsets[i]->rk.type) == 323933707f3Ssthen LDNS_RR_TYPE_NSEC3) { 324933707f3Ssthen val_find_rrset_signer(rep->rrsets[i], 325933707f3Ssthen signer_name, signer_len); 326933707f3Ssthen return; 327933707f3Ssthen } 328933707f3Ssthen } 329933707f3Ssthen } else if(subtype == VAL_CLASS_CNAMENOANSWER) { 330933707f3Ssthen /* find closest superdomain signer name in authority section 331933707f3Ssthen * NSEC and NSEC3s */ 332933707f3Ssthen int matchcount = 0; 333933707f3Ssthen *signer_name = NULL; 334933707f3Ssthen *signer_len = 0; 335933707f3Ssthen for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep-> 336933707f3Ssthen ns_numrrsets; i++) { 337933707f3Ssthen if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_NSEC 338933707f3Ssthen || ntohs(rep->rrsets[i]->rk.type) == 339933707f3Ssthen LDNS_RR_TYPE_NSEC3) { 340933707f3Ssthen val_find_best_signer(rep->rrsets[i], qinf, 341933707f3Ssthen signer_name, signer_len, &matchcount); 342933707f3Ssthen } 343933707f3Ssthen } 34477079be7Ssthen } else if(subtype == VAL_CLASS_ANY) { 34577079be7Ssthen /* check for one of the answer rrset that has signatures, 34677079be7Ssthen * or potentially a DNAME is in use with a different qname */ 34777079be7Ssthen for(i=skip; i<rep->an_numrrsets; i++) { 34877079be7Ssthen if(query_dname_compare(qinf->qname, 34977079be7Ssthen rep->rrsets[i]->rk.dname) == 0) { 35077079be7Ssthen val_find_rrset_signer(rep->rrsets[i], 35177079be7Ssthen signer_name, signer_len); 35277079be7Ssthen if(*signer_name) 35377079be7Ssthen return; 35477079be7Ssthen } 35577079be7Ssthen } 35677079be7Ssthen /* no answer RRSIGs with qname, try a DNAME */ 35777079be7Ssthen if(skip < rep->an_numrrsets && 35877079be7Ssthen ntohs(rep->rrsets[skip]->rk.type) == 35977079be7Ssthen LDNS_RR_TYPE_DNAME) { 36077079be7Ssthen val_find_rrset_signer(rep->rrsets[skip], 36177079be7Ssthen signer_name, signer_len); 36277079be7Ssthen if(*signer_name) 36377079be7Ssthen return; 36477079be7Ssthen } 36577079be7Ssthen *signer_name = NULL; 36677079be7Ssthen *signer_len = 0; 367933707f3Ssthen } else if(subtype == VAL_CLASS_REFERRAL) { 368933707f3Ssthen /* find keys for the item at skip */ 369933707f3Ssthen if(skip < rep->rrset_count) { 370933707f3Ssthen val_find_rrset_signer(rep->rrsets[skip], 371933707f3Ssthen signer_name, signer_len); 372933707f3Ssthen return; 373933707f3Ssthen } 374933707f3Ssthen *signer_name = NULL; 375933707f3Ssthen *signer_len = 0; 376933707f3Ssthen } else { 377933707f3Ssthen verbose(VERB_QUERY, "find_signer: could not find signer name" 378933707f3Ssthen " for unknown type response"); 379933707f3Ssthen *signer_name = NULL; 380933707f3Ssthen *signer_len = 0; 381933707f3Ssthen } 382933707f3Ssthen } 383933707f3Ssthen 384933707f3Ssthen /** return number of rrs in an rrset */ 385933707f3Ssthen static size_t 386933707f3Ssthen rrset_get_count(struct ub_packed_rrset_key* rrset) 387933707f3Ssthen { 388933707f3Ssthen struct packed_rrset_data* d = (struct packed_rrset_data*) 389933707f3Ssthen rrset->entry.data; 390933707f3Ssthen if(!d) return 0; 391933707f3Ssthen return d->count; 392933707f3Ssthen } 393933707f3Ssthen 394933707f3Ssthen /** return TTL of rrset */ 395933707f3Ssthen static uint32_t 396933707f3Ssthen rrset_get_ttl(struct ub_packed_rrset_key* rrset) 397933707f3Ssthen { 398933707f3Ssthen struct packed_rrset_data* d = (struct packed_rrset_data*) 399933707f3Ssthen rrset->entry.data; 400933707f3Ssthen if(!d) return 0; 401933707f3Ssthen return d->ttl; 402933707f3Ssthen } 403933707f3Ssthen 4040bdb4f62Ssthen static enum sec_status 405933707f3Ssthen val_verify_rrset(struct module_env* env, struct val_env* ve, 406933707f3Ssthen struct ub_packed_rrset_key* rrset, struct ub_packed_rrset_key* keys, 4070bdb4f62Ssthen uint8_t* sigalg, char** reason, sldns_ede_code *reason_bogus, 408817bdb8fSflorian sldns_pkt_section section, struct module_qstate* qstate, 409*98bc733bSsthen int *verified, char* reasonbuf, size_t reasonlen) 410933707f3Ssthen { 411933707f3Ssthen enum sec_status sec; 412933707f3Ssthen struct packed_rrset_data* d = (struct packed_rrset_data*)rrset-> 413933707f3Ssthen entry.data; 414933707f3Ssthen if(d->security == sec_status_secure) { 415933707f3Ssthen /* re-verify all other statuses, because keyset may change*/ 416933707f3Ssthen log_nametypeclass(VERB_ALGO, "verify rrset cached", 417933707f3Ssthen rrset->rk.dname, ntohs(rrset->rk.type), 418933707f3Ssthen ntohs(rrset->rk.rrset_class)); 419817bdb8fSflorian *verified = 0; 420933707f3Ssthen return d->security; 421933707f3Ssthen } 422933707f3Ssthen /* check in the cache if verification has already been done */ 423933707f3Ssthen rrset_check_sec_status(env->rrset_cache, rrset, *env->now); 424933707f3Ssthen if(d->security == sec_status_secure) { 425933707f3Ssthen log_nametypeclass(VERB_ALGO, "verify rrset from cache", 426933707f3Ssthen rrset->rk.dname, ntohs(rrset->rk.type), 427933707f3Ssthen ntohs(rrset->rk.rrset_class)); 428817bdb8fSflorian *verified = 0; 429933707f3Ssthen return d->security; 430933707f3Ssthen } 431933707f3Ssthen log_nametypeclass(VERB_ALGO, "verify rrset", rrset->rk.dname, 432933707f3Ssthen ntohs(rrset->rk.type), ntohs(rrset->rk.rrset_class)); 433bdfc4d55Sflorian sec = dnskeyset_verify_rrset(env, ve, rrset, keys, sigalg, reason, 434*98bc733bSsthen reason_bogus, section, qstate, verified, reasonbuf, reasonlen); 435933707f3Ssthen verbose(VERB_ALGO, "verify result: %s", sec_status_to_string(sec)); 436933707f3Ssthen regional_free_all(env->scratch); 437933707f3Ssthen 438933707f3Ssthen /* update rrset security status 439933707f3Ssthen * only improves security status 440933707f3Ssthen * and bogus is set only once, even if we rechecked the status */ 441933707f3Ssthen if(sec > d->security) { 442933707f3Ssthen d->security = sec; 443933707f3Ssthen if(sec == sec_status_secure) 444933707f3Ssthen d->trust = rrset_trust_validated; 445933707f3Ssthen else if(sec == sec_status_bogus) { 446933707f3Ssthen size_t i; 447933707f3Ssthen /* update ttl for rrset to fixed value. */ 448933707f3Ssthen d->ttl = ve->bogus_ttl; 449933707f3Ssthen for(i=0; i<d->count+d->rrsig_count; i++) 450933707f3Ssthen d->rr_ttl[i] = ve->bogus_ttl; 451933707f3Ssthen /* leave RR specific TTL: not used for determine 452933707f3Ssthen * if RRset timed out and clients see proper value. */ 453933707f3Ssthen lock_basic_lock(&ve->bogus_lock); 454933707f3Ssthen ve->num_rrset_bogus++; 455933707f3Ssthen lock_basic_unlock(&ve->bogus_lock); 456933707f3Ssthen } 457933707f3Ssthen /* if status updated - store in cache for reuse */ 458933707f3Ssthen rrset_update_sec_status(env->rrset_cache, rrset, *env->now); 459933707f3Ssthen } 460933707f3Ssthen 461933707f3Ssthen return sec; 462933707f3Ssthen } 463933707f3Ssthen 464933707f3Ssthen enum sec_status 465933707f3Ssthen val_verify_rrset_entry(struct module_env* env, struct val_env* ve, 466933707f3Ssthen struct ub_packed_rrset_key* rrset, struct key_entry_key* kkey, 4670bdb4f62Ssthen char** reason, sldns_ede_code *reason_bogus, 468817bdb8fSflorian sldns_pkt_section section, struct module_qstate* qstate, 469*98bc733bSsthen int* verified, char* reasonbuf, size_t reasonlen) 470933707f3Ssthen { 471933707f3Ssthen /* temporary dnskey rrset-key */ 472933707f3Ssthen struct ub_packed_rrset_key dnskey; 473933707f3Ssthen struct key_entry_data* kd = (struct key_entry_data*)kkey->entry.data; 474933707f3Ssthen enum sec_status sec; 475933707f3Ssthen dnskey.rk.type = htons(kd->rrset_type); 476933707f3Ssthen dnskey.rk.rrset_class = htons(kkey->key_class); 477933707f3Ssthen dnskey.rk.flags = 0; 478933707f3Ssthen dnskey.rk.dname = kkey->name; 479933707f3Ssthen dnskey.rk.dname_len = kkey->namelen; 480933707f3Ssthen dnskey.entry.key = &dnskey; 481933707f3Ssthen dnskey.entry.data = kd->rrset_data; 482bdfc4d55Sflorian sec = val_verify_rrset(env, ve, rrset, &dnskey, kd->algo, reason, 483*98bc733bSsthen reason_bogus, section, qstate, verified, reasonbuf, reasonlen); 484933707f3Ssthen return sec; 485933707f3Ssthen } 486933707f3Ssthen 487933707f3Ssthen /** verify that a DS RR hashes to a key and that key signs the set */ 488933707f3Ssthen static enum sec_status 489933707f3Ssthen verify_dnskeys_with_ds_rr(struct module_env* env, struct val_env* ve, 490933707f3Ssthen struct ub_packed_rrset_key* dnskey_rrset, 491bdfc4d55Sflorian struct ub_packed_rrset_key* ds_rrset, size_t ds_idx, char** reason, 492f46c52bfSsthen sldns_ede_code *reason_bogus, struct module_qstate* qstate, 493*98bc733bSsthen int *nonechecked, char* reasonbuf, size_t reasonlen) 494933707f3Ssthen { 495933707f3Ssthen enum sec_status sec = sec_status_bogus; 496191f22c6Ssthen size_t i, num, numchecked = 0, numhashok = 0, numsizesupp = 0; 497933707f3Ssthen num = rrset_get_count(dnskey_rrset); 498f46c52bfSsthen *nonechecked = 0; 499933707f3Ssthen for(i=0; i<num; i++) { 500933707f3Ssthen /* Skip DNSKEYs that don't match the basic criteria. */ 501933707f3Ssthen if(ds_get_key_algo(ds_rrset, ds_idx) 502933707f3Ssthen != dnskey_get_algo(dnskey_rrset, i) 503933707f3Ssthen || dnskey_calc_keytag(dnskey_rrset, i) 504933707f3Ssthen != ds_get_keytag(ds_rrset, ds_idx)) { 505933707f3Ssthen continue; 506933707f3Ssthen } 507933707f3Ssthen numchecked++; 508933707f3Ssthen verbose(VERB_ALGO, "attempt DS match algo %d keytag %d", 509933707f3Ssthen ds_get_key_algo(ds_rrset, ds_idx), 510933707f3Ssthen ds_get_keytag(ds_rrset, ds_idx)); 511933707f3Ssthen 512933707f3Ssthen /* Convert the candidate DNSKEY into a hash using the 513933707f3Ssthen * same DS hash algorithm. */ 514933707f3Ssthen if(!ds_digest_match_dnskey(env, dnskey_rrset, i, ds_rrset, 515933707f3Ssthen ds_idx)) { 516933707f3Ssthen verbose(VERB_ALGO, "DS match attempt failed"); 517817bdb8fSflorian if(numchecked > numhashok + MAX_DS_MATCH_FAILURES) { 518817bdb8fSflorian verbose(VERB_ALGO, "DS match attempt reached " 519817bdb8fSflorian "MAX_DS_MATCH_FAILURES (%d); bogus", 520817bdb8fSflorian MAX_DS_MATCH_FAILURES); 521817bdb8fSflorian return sec_status_bogus; 522817bdb8fSflorian } 523933707f3Ssthen continue; 524933707f3Ssthen } 525933707f3Ssthen numhashok++; 526191f22c6Ssthen if(!dnskey_size_is_supported(dnskey_rrset, i)) { 527191f22c6Ssthen verbose(VERB_ALGO, "DS okay but that DNSKEY size is not supported"); 528191f22c6Ssthen numsizesupp++; 529191f22c6Ssthen continue; 530191f22c6Ssthen } 531933707f3Ssthen verbose(VERB_ALGO, "DS match digest ok, trying signature"); 532933707f3Ssthen 533933707f3Ssthen /* Otherwise, we have a match! Make sure that the DNSKEY 534933707f3Ssthen * verifies *with this key* */ 5350bdb4f62Ssthen sec = dnskey_verify_rrset(env, ve, dnskey_rrset, dnskey_rrset, 5360bdb4f62Ssthen i, reason, reason_bogus, LDNS_SECTION_ANSWER, qstate); 537933707f3Ssthen if(sec == sec_status_secure) { 538933707f3Ssthen return sec; 539933707f3Ssthen } 540933707f3Ssthen /* If it didn't validate with the DNSKEY, try the next one! */ 541933707f3Ssthen } 542d1e2768aSsthen if(numsizesupp != 0 || sec == sec_status_indeterminate) { 543191f22c6Ssthen /* there is a working DS, but that DNSKEY is not supported */ 544191f22c6Ssthen return sec_status_insecure; 545191f22c6Ssthen } 546f46c52bfSsthen if(numchecked == 0) { 547*98bc733bSsthen algo_needs_reason(ds_get_key_algo(ds_rrset, ds_idx), 548*98bc733bSsthen reason, "no keys have a DS", reasonbuf, reasonlen); 549f46c52bfSsthen *nonechecked = 1; 550f46c52bfSsthen } else if(numhashok == 0) { 551933707f3Ssthen *reason = "DS hash mismatches key"; 552f46c52bfSsthen } else if(!*reason) { 553933707f3Ssthen *reason = "keyset not secured by DNSKEY that matches DS"; 554f46c52bfSsthen } 555933707f3Ssthen return sec_status_bogus; 556933707f3Ssthen } 557933707f3Ssthen 558933707f3Ssthen int val_favorite_ds_algo(struct ub_packed_rrset_key* ds_rrset) 559933707f3Ssthen { 560933707f3Ssthen size_t i, num = rrset_get_count(ds_rrset); 561933707f3Ssthen int d, digest_algo = 0; /* DS digest algo 0 is not used. */ 562933707f3Ssthen /* find favorite algo, for now, highest number supported */ 563933707f3Ssthen for(i=0; i<num; i++) { 564933707f3Ssthen if(!ds_digest_algo_is_supported(ds_rrset, i) || 565933707f3Ssthen !ds_key_algo_is_supported(ds_rrset, i)) { 566933707f3Ssthen continue; 567933707f3Ssthen } 568933707f3Ssthen d = ds_get_digest_algo(ds_rrset, i); 569933707f3Ssthen if(d > digest_algo) 570933707f3Ssthen digest_algo = d; 571933707f3Ssthen } 572933707f3Ssthen return digest_algo; 573933707f3Ssthen } 574933707f3Ssthen 575933707f3Ssthen enum sec_status 576933707f3Ssthen val_verify_DNSKEY_with_DS(struct module_env* env, struct val_env* ve, 577933707f3Ssthen struct ub_packed_rrset_key* dnskey_rrset, 578bdfc4d55Sflorian struct ub_packed_rrset_key* ds_rrset, uint8_t* sigalg, char** reason, 579*98bc733bSsthen sldns_ede_code *reason_bogus, struct module_qstate* qstate, 580*98bc733bSsthen char* reasonbuf, size_t reasonlen) 581933707f3Ssthen { 582933707f3Ssthen /* as long as this is false, we can consider this DS rrset to be 583933707f3Ssthen * equivalent to no DS rrset. */ 584f46c52bfSsthen int has_useful_ds = 0, digest_algo, alg, has_algo_refusal = 0, 585f46c52bfSsthen nonechecked, has_checked_ds = 0; 586933707f3Ssthen struct algo_needs needs; 587933707f3Ssthen size_t i, num; 588933707f3Ssthen enum sec_status sec; 589933707f3Ssthen 590933707f3Ssthen if(dnskey_rrset->rk.dname_len != ds_rrset->rk.dname_len || 591933707f3Ssthen query_dname_compare(dnskey_rrset->rk.dname, ds_rrset->rk.dname) 592933707f3Ssthen != 0) { 593933707f3Ssthen verbose(VERB_QUERY, "DNSKEY RRset did not match DS RRset " 594933707f3Ssthen "by name"); 595933707f3Ssthen *reason = "DNSKEY RRset did not match DS RRset by name"; 596933707f3Ssthen return sec_status_bogus; 597933707f3Ssthen } 598933707f3Ssthen 5992be9e038Ssthen if(sigalg) { 6002be9e038Ssthen /* harden against algo downgrade is enabled */ 601933707f3Ssthen digest_algo = val_favorite_ds_algo(ds_rrset); 602933707f3Ssthen algo_needs_init_ds(&needs, ds_rrset, digest_algo, sigalg); 6032be9e038Ssthen } else { 6042be9e038Ssthen /* accept any key algo, any digest algo */ 6052be9e038Ssthen digest_algo = -1; 6062be9e038Ssthen } 607933707f3Ssthen num = rrset_get_count(ds_rrset); 608933707f3Ssthen for(i=0; i<num; i++) { 609933707f3Ssthen /* Check to see if we can understand this DS. 610933707f3Ssthen * And check it is the strongest digest */ 611933707f3Ssthen if(!ds_digest_algo_is_supported(ds_rrset, i) || 612933707f3Ssthen !ds_key_algo_is_supported(ds_rrset, i) || 6132be9e038Ssthen (sigalg && (ds_get_digest_algo(ds_rrset, i) != digest_algo))) { 614933707f3Ssthen continue; 615933707f3Ssthen } 616933707f3Ssthen 617191f22c6Ssthen sec = verify_dnskeys_with_ds_rr(env, ve, dnskey_rrset, 618f46c52bfSsthen ds_rrset, i, reason, reason_bogus, qstate, 619*98bc733bSsthen &nonechecked, reasonbuf, reasonlen); 620f46c52bfSsthen if(sec == sec_status_insecure) { 621f46c52bfSsthen /* DNSKEY too large unsupported or algo refused by 622f46c52bfSsthen * crypto lib. */ 623f46c52bfSsthen has_algo_refusal = 1; 624191f22c6Ssthen continue; 625f46c52bfSsthen } 626f46c52bfSsthen if(!nonechecked) 627f46c52bfSsthen has_checked_ds = 1; 628191f22c6Ssthen 629933707f3Ssthen /* Once we see a single DS with a known digestID and 630933707f3Ssthen * algorithm, we cannot return INSECURE (with a 631933707f3Ssthen * "null" KeyEntry). */ 632229e174cSsthen has_useful_ds = 1; 633933707f3Ssthen 634933707f3Ssthen if(sec == sec_status_secure) { 635933707f3Ssthen if(!sigalg || algo_needs_set_secure(&needs, 636933707f3Ssthen (uint8_t)ds_get_key_algo(ds_rrset, i))) { 637933707f3Ssthen verbose(VERB_ALGO, "DS matched DNSKEY."); 638191f22c6Ssthen if(!dnskeyset_size_is_supported(dnskey_rrset)) { 639191f22c6Ssthen verbose(VERB_ALGO, "DS works, but dnskeyset contain keys that are unsupported, treat as insecure"); 640191f22c6Ssthen return sec_status_insecure; 641191f22c6Ssthen } 642933707f3Ssthen return sec_status_secure; 643933707f3Ssthen } 644933707f3Ssthen } else if(sigalg && sec == sec_status_bogus) { 645933707f3Ssthen algo_needs_set_bogus(&needs, 646933707f3Ssthen (uint8_t)ds_get_key_algo(ds_rrset, i)); 647933707f3Ssthen } 648933707f3Ssthen } 649933707f3Ssthen 650933707f3Ssthen /* None of the DS's worked out. */ 651933707f3Ssthen 652f46c52bfSsthen /* If none of the DSes have been checked, eg. that means no matches 653f46c52bfSsthen * for keytags, and the other dses are all algo_refusal, it is an 654f46c52bfSsthen * insecure delegation point, since the only matched DS records 655f46c52bfSsthen * have an algo refusal, or are unsupported. */ 656f46c52bfSsthen if(has_algo_refusal && !has_checked_ds) { 657f46c52bfSsthen verbose(VERB_ALGO, "No supported DS records were found -- " 658f46c52bfSsthen "treating as insecure."); 659f46c52bfSsthen return sec_status_insecure; 660f46c52bfSsthen } 661933707f3Ssthen /* If no DSs were understandable, then this is OK. */ 662933707f3Ssthen if(!has_useful_ds) { 663933707f3Ssthen verbose(VERB_ALGO, "No usable DS records were found -- " 664933707f3Ssthen "treating as insecure."); 665933707f3Ssthen return sec_status_insecure; 666933707f3Ssthen } 667933707f3Ssthen /* If any were understandable, then it is bad. */ 668933707f3Ssthen verbose(VERB_QUERY, "Failed to match any usable DS to a DNSKEY."); 669933707f3Ssthen if(sigalg && (alg=algo_needs_missing(&needs)) != 0) { 670*98bc733bSsthen algo_needs_reason(alg, reason, "missing verification of " 671*98bc733bSsthen "DNSKEY signature", reasonbuf, reasonlen); 672933707f3Ssthen } 673933707f3Ssthen return sec_status_bogus; 674933707f3Ssthen } 675933707f3Ssthen 676933707f3Ssthen struct key_entry_key* 677933707f3Ssthen val_verify_new_DNSKEYs(struct regional* region, struct module_env* env, 678933707f3Ssthen struct val_env* ve, struct ub_packed_rrset_key* dnskey_rrset, 679bdfc4d55Sflorian struct ub_packed_rrset_key* ds_rrset, int downprot, char** reason, 680*98bc733bSsthen sldns_ede_code *reason_bogus, struct module_qstate* qstate, 681*98bc733bSsthen char* reasonbuf, size_t reasonlen) 682933707f3Ssthen { 683933707f3Ssthen uint8_t sigalg[ALGO_NEEDS_MAX+1]; 684933707f3Ssthen enum sec_status sec = val_verify_DNSKEY_with_DS(env, ve, 6850bdb4f62Ssthen dnskey_rrset, ds_rrset, downprot?sigalg:NULL, reason, 686*98bc733bSsthen reason_bogus, qstate, reasonbuf, reasonlen); 687933707f3Ssthen 688933707f3Ssthen if(sec == sec_status_secure) { 689933707f3Ssthen return key_entry_create_rrset(region, 690933707f3Ssthen ds_rrset->rk.dname, ds_rrset->rk.dname_len, 691933707f3Ssthen ntohs(ds_rrset->rk.rrset_class), dnskey_rrset, 6928b7325afSsthen downprot?sigalg:NULL, LDNS_EDE_NONE, NULL, 6938b7325afSsthen *env->now); 694933707f3Ssthen } else if(sec == sec_status_insecure) { 695933707f3Ssthen return key_entry_create_null(region, ds_rrset->rk.dname, 696933707f3Ssthen ds_rrset->rk.dname_len, 697933707f3Ssthen ntohs(ds_rrset->rk.rrset_class), 6988b7325afSsthen rrset_get_ttl(ds_rrset), *reason_bogus, *reason, 6998b7325afSsthen *env->now); 700933707f3Ssthen } 701933707f3Ssthen return key_entry_create_bad(region, ds_rrset->rk.dname, 702933707f3Ssthen ds_rrset->rk.dname_len, ntohs(ds_rrset->rk.rrset_class), 7038b7325afSsthen BOGUS_KEY_TTL, *reason_bogus, *reason, *env->now); 704933707f3Ssthen } 705933707f3Ssthen 706933707f3Ssthen enum sec_status 707933707f3Ssthen val_verify_DNSKEY_with_TA(struct module_env* env, struct val_env* ve, 708933707f3Ssthen struct ub_packed_rrset_key* dnskey_rrset, 709933707f3Ssthen struct ub_packed_rrset_key* ta_ds, 710bdfc4d55Sflorian struct ub_packed_rrset_key* ta_dnskey, uint8_t* sigalg, char** reason, 711*98bc733bSsthen sldns_ede_code *reason_bogus, struct module_qstate* qstate, 712*98bc733bSsthen char* reasonbuf, size_t reasonlen) 713933707f3Ssthen { 714933707f3Ssthen /* as long as this is false, we can consider this anchor to be 715933707f3Ssthen * equivalent to no anchor. */ 716f46c52bfSsthen int has_useful_ta = 0, digest_algo = 0, alg, has_algo_refusal = 0, 717f46c52bfSsthen nonechecked, has_checked_ds = 0; 718933707f3Ssthen struct algo_needs needs; 719933707f3Ssthen size_t i, num; 720933707f3Ssthen enum sec_status sec; 721933707f3Ssthen 722933707f3Ssthen if(ta_ds && (dnskey_rrset->rk.dname_len != ta_ds->rk.dname_len || 723933707f3Ssthen query_dname_compare(dnskey_rrset->rk.dname, ta_ds->rk.dname) 724933707f3Ssthen != 0)) { 725933707f3Ssthen verbose(VERB_QUERY, "DNSKEY RRset did not match DS RRset " 726933707f3Ssthen "by name"); 727933707f3Ssthen *reason = "DNSKEY RRset did not match DS RRset by name"; 7280bdb4f62Ssthen if(reason_bogus) 7290bdb4f62Ssthen *reason_bogus = LDNS_EDE_DNSKEY_MISSING; 730933707f3Ssthen return sec_status_bogus; 731933707f3Ssthen } 732933707f3Ssthen if(ta_dnskey && (dnskey_rrset->rk.dname_len != ta_dnskey->rk.dname_len 733933707f3Ssthen || query_dname_compare(dnskey_rrset->rk.dname, ta_dnskey->rk.dname) 734933707f3Ssthen != 0)) { 735933707f3Ssthen verbose(VERB_QUERY, "DNSKEY RRset did not match anchor RRset " 736933707f3Ssthen "by name"); 737933707f3Ssthen *reason = "DNSKEY RRset did not match anchor RRset by name"; 7380bdb4f62Ssthen if(reason_bogus) 7390bdb4f62Ssthen *reason_bogus = LDNS_EDE_DNSKEY_MISSING; 740933707f3Ssthen return sec_status_bogus; 741933707f3Ssthen } 742933707f3Ssthen 743933707f3Ssthen if(ta_ds) 744933707f3Ssthen digest_algo = val_favorite_ds_algo(ta_ds); 745933707f3Ssthen if(sigalg) { 746933707f3Ssthen if(ta_ds) 747933707f3Ssthen algo_needs_init_ds(&needs, ta_ds, digest_algo, sigalg); 748933707f3Ssthen else memset(&needs, 0, sizeof(needs)); 749933707f3Ssthen if(ta_dnskey) 750933707f3Ssthen algo_needs_init_dnskey_add(&needs, ta_dnskey, sigalg); 751933707f3Ssthen } 752933707f3Ssthen if(ta_ds) { 753933707f3Ssthen num = rrset_get_count(ta_ds); 754933707f3Ssthen for(i=0; i<num; i++) { 755933707f3Ssthen /* Check to see if we can understand this DS. 756933707f3Ssthen * And check it is the strongest digest */ 757933707f3Ssthen if(!ds_digest_algo_is_supported(ta_ds, i) || 758933707f3Ssthen !ds_key_algo_is_supported(ta_ds, i) || 759933707f3Ssthen ds_get_digest_algo(ta_ds, i) != digest_algo) 760933707f3Ssthen continue; 761933707f3Ssthen 762191f22c6Ssthen sec = verify_dnskeys_with_ds_rr(env, ve, dnskey_rrset, 763*98bc733bSsthen ta_ds, i, reason, reason_bogus, qstate, &nonechecked, 764*98bc733bSsthen reasonbuf, reasonlen); 765f46c52bfSsthen if(sec == sec_status_insecure) { 766f46c52bfSsthen has_algo_refusal = 1; 767191f22c6Ssthen continue; 768f46c52bfSsthen } 769f46c52bfSsthen if(!nonechecked) 770f46c52bfSsthen has_checked_ds = 1; 771191f22c6Ssthen 772933707f3Ssthen /* Once we see a single DS with a known digestID and 773933707f3Ssthen * algorithm, we cannot return INSECURE (with a 774933707f3Ssthen * "null" KeyEntry). */ 775229e174cSsthen has_useful_ta = 1; 776933707f3Ssthen 777933707f3Ssthen if(sec == sec_status_secure) { 778933707f3Ssthen if(!sigalg || algo_needs_set_secure(&needs, 779933707f3Ssthen (uint8_t)ds_get_key_algo(ta_ds, i))) { 780933707f3Ssthen verbose(VERB_ALGO, "DS matched DNSKEY."); 781191f22c6Ssthen if(!dnskeyset_size_is_supported(dnskey_rrset)) { 782191f22c6Ssthen verbose(VERB_ALGO, "trustanchor works, but dnskeyset contain keys that are unsupported, treat as insecure"); 783191f22c6Ssthen return sec_status_insecure; 784191f22c6Ssthen } 785933707f3Ssthen return sec_status_secure; 786933707f3Ssthen } 787933707f3Ssthen } else if(sigalg && sec == sec_status_bogus) { 788933707f3Ssthen algo_needs_set_bogus(&needs, 789933707f3Ssthen (uint8_t)ds_get_key_algo(ta_ds, i)); 790933707f3Ssthen } 791933707f3Ssthen } 792933707f3Ssthen } 793933707f3Ssthen 794933707f3Ssthen /* None of the DS's worked out: check the DNSKEYs. */ 795933707f3Ssthen if(ta_dnskey) { 796933707f3Ssthen num = rrset_get_count(ta_dnskey); 797933707f3Ssthen for(i=0; i<num; i++) { 798933707f3Ssthen /* Check to see if we can understand this DNSKEY */ 799933707f3Ssthen if(!dnskey_algo_is_supported(ta_dnskey, i)) 800933707f3Ssthen continue; 801191f22c6Ssthen if(!dnskey_size_is_supported(ta_dnskey, i)) 802191f22c6Ssthen continue; 803933707f3Ssthen 804933707f3Ssthen /* we saw a useful TA */ 805229e174cSsthen has_useful_ta = 1; 806933707f3Ssthen 807933707f3Ssthen sec = dnskey_verify_rrset(env, ve, dnskey_rrset, 8088b7325afSsthen ta_dnskey, i, reason, reason_bogus, LDNS_SECTION_ANSWER, qstate); 809933707f3Ssthen if(sec == sec_status_secure) { 810933707f3Ssthen if(!sigalg || algo_needs_set_secure(&needs, 811933707f3Ssthen (uint8_t)dnskey_get_algo(ta_dnskey, i))) { 812933707f3Ssthen verbose(VERB_ALGO, "anchor matched DNSKEY."); 813191f22c6Ssthen if(!dnskeyset_size_is_supported(dnskey_rrset)) { 814191f22c6Ssthen verbose(VERB_ALGO, "trustanchor works, but dnskeyset contain keys that are unsupported, treat as insecure"); 815191f22c6Ssthen return sec_status_insecure; 816191f22c6Ssthen } 817933707f3Ssthen return sec_status_secure; 818933707f3Ssthen } 819933707f3Ssthen } else if(sigalg && sec == sec_status_bogus) { 820933707f3Ssthen algo_needs_set_bogus(&needs, 821933707f3Ssthen (uint8_t)dnskey_get_algo(ta_dnskey, i)); 822933707f3Ssthen } 823933707f3Ssthen } 824933707f3Ssthen } 825933707f3Ssthen 826f46c52bfSsthen /* If none of the DSes have been checked, eg. that means no matches 827f46c52bfSsthen * for keytags, and the other dses are all algo_refusal, it is an 828f46c52bfSsthen * insecure delegation point, since the only matched DS records 829f46c52bfSsthen * have an algo refusal, or are unsupported. */ 830f46c52bfSsthen if(has_algo_refusal && !has_checked_ds) { 831f46c52bfSsthen verbose(VERB_ALGO, "No supported trust anchors were found -- " 832f46c52bfSsthen "treating as insecure."); 833f46c52bfSsthen return sec_status_insecure; 834f46c52bfSsthen } 835933707f3Ssthen /* If no DSs were understandable, then this is OK. */ 836933707f3Ssthen if(!has_useful_ta) { 837933707f3Ssthen verbose(VERB_ALGO, "No usable trust anchors were found -- " 838933707f3Ssthen "treating as insecure."); 839933707f3Ssthen return sec_status_insecure; 840933707f3Ssthen } 841933707f3Ssthen /* If any were understandable, then it is bad. */ 842933707f3Ssthen verbose(VERB_QUERY, "Failed to match any usable anchor to a DNSKEY."); 843933707f3Ssthen if(sigalg && (alg=algo_needs_missing(&needs)) != 0) { 844*98bc733bSsthen algo_needs_reason(alg, reason, "missing verification of " 845*98bc733bSsthen "DNSKEY signature", reasonbuf, reasonlen); 846933707f3Ssthen } 847933707f3Ssthen return sec_status_bogus; 848933707f3Ssthen } 849933707f3Ssthen 850933707f3Ssthen struct key_entry_key* 851933707f3Ssthen val_verify_new_DNSKEYs_with_ta(struct regional* region, struct module_env* env, 852933707f3Ssthen struct val_env* ve, struct ub_packed_rrset_key* dnskey_rrset, 853933707f3Ssthen struct ub_packed_rrset_key* ta_ds_rrset, 854933707f3Ssthen struct ub_packed_rrset_key* ta_dnskey_rrset, int downprot, 855*98bc733bSsthen char** reason, sldns_ede_code *reason_bogus, 856*98bc733bSsthen struct module_qstate* qstate, char* reasonbuf, size_t reasonlen) 857933707f3Ssthen { 858933707f3Ssthen uint8_t sigalg[ALGO_NEEDS_MAX+1]; 859933707f3Ssthen enum sec_status sec = val_verify_DNSKEY_with_TA(env, ve, 860933707f3Ssthen dnskey_rrset, ta_ds_rrset, ta_dnskey_rrset, 861*98bc733bSsthen downprot?sigalg:NULL, reason, reason_bogus, qstate, 862*98bc733bSsthen reasonbuf, reasonlen); 863933707f3Ssthen 864933707f3Ssthen if(sec == sec_status_secure) { 865933707f3Ssthen return key_entry_create_rrset(region, 866933707f3Ssthen dnskey_rrset->rk.dname, dnskey_rrset->rk.dname_len, 867933707f3Ssthen ntohs(dnskey_rrset->rk.rrset_class), dnskey_rrset, 8688b7325afSsthen downprot?sigalg:NULL, LDNS_EDE_NONE, NULL, *env->now); 869933707f3Ssthen } else if(sec == sec_status_insecure) { 870933707f3Ssthen return key_entry_create_null(region, dnskey_rrset->rk.dname, 871933707f3Ssthen dnskey_rrset->rk.dname_len, 872933707f3Ssthen ntohs(dnskey_rrset->rk.rrset_class), 8738b7325afSsthen rrset_get_ttl(dnskey_rrset), *reason_bogus, *reason, 8748b7325afSsthen *env->now); 875933707f3Ssthen } 876933707f3Ssthen return key_entry_create_bad(region, dnskey_rrset->rk.dname, 877933707f3Ssthen dnskey_rrset->rk.dname_len, ntohs(dnskey_rrset->rk.rrset_class), 8788b7325afSsthen BOGUS_KEY_TTL, *reason_bogus, *reason, *env->now); 879933707f3Ssthen } 880933707f3Ssthen 881933707f3Ssthen int 882933707f3Ssthen val_dsset_isusable(struct ub_packed_rrset_key* ds_rrset) 883933707f3Ssthen { 884933707f3Ssthen size_t i; 885933707f3Ssthen for(i=0; i<rrset_get_count(ds_rrset); i++) { 886933707f3Ssthen if(ds_digest_algo_is_supported(ds_rrset, i) && 887933707f3Ssthen ds_key_algo_is_supported(ds_rrset, i)) 888933707f3Ssthen return 1; 889933707f3Ssthen } 89032e31f52Ssthen if(verbosity < VERB_ALGO) 89132e31f52Ssthen return 0; 89232e31f52Ssthen if(rrset_get_count(ds_rrset) == 0) 89332e31f52Ssthen verbose(VERB_ALGO, "DS is not usable"); 89432e31f52Ssthen else { 89532e31f52Ssthen /* report usability for the first DS RR */ 89632e31f52Ssthen sldns_lookup_table *lt; 89732e31f52Ssthen char herr[64], aerr[64]; 89832e31f52Ssthen lt = sldns_lookup_by_id(sldns_hashes, 899e21c60efSsthen (int)ds_get_digest_algo(ds_rrset, 0)); 90032e31f52Ssthen if(lt) snprintf(herr, sizeof(herr), "%s", lt->name); 90132e31f52Ssthen else snprintf(herr, sizeof(herr), "%d", 902e21c60efSsthen (int)ds_get_digest_algo(ds_rrset, 0)); 90332e31f52Ssthen lt = sldns_lookup_by_id(sldns_algorithms, 904e21c60efSsthen (int)ds_get_key_algo(ds_rrset, 0)); 90532e31f52Ssthen if(lt) snprintf(aerr, sizeof(aerr), "%s", lt->name); 90632e31f52Ssthen else snprintf(aerr, sizeof(aerr), "%d", 907e21c60efSsthen (int)ds_get_key_algo(ds_rrset, 0)); 9080bdb4f62Ssthen 90932e31f52Ssthen verbose(VERB_ALGO, "DS unsupported, hash %s %s, " 91032e31f52Ssthen "key algorithm %s %s", herr, 91132e31f52Ssthen (ds_digest_algo_is_supported(ds_rrset, 0)? 91232e31f52Ssthen "(supported)":"(unsupported)"), aerr, 91332e31f52Ssthen (ds_key_algo_is_supported(ds_rrset, 0)? 91432e31f52Ssthen "(supported)":"(unsupported)")); 91532e31f52Ssthen } 916933707f3Ssthen return 0; 917933707f3Ssthen } 918933707f3Ssthen 919933707f3Ssthen /** get label count for a signature */ 920933707f3Ssthen static uint8_t 921933707f3Ssthen rrsig_get_labcount(struct packed_rrset_data* d, size_t sig) 922933707f3Ssthen { 923933707f3Ssthen if(d->rr_len[sig] < 2+4) 924933707f3Ssthen return 0; /* bad sig length */ 925933707f3Ssthen return d->rr_data[sig][2+3]; 926933707f3Ssthen } 927933707f3Ssthen 928933707f3Ssthen int 929938a3a5eSflorian val_rrset_wildcard(struct ub_packed_rrset_key* rrset, uint8_t** wc, 930938a3a5eSflorian size_t* wc_len) 931933707f3Ssthen { 932933707f3Ssthen struct packed_rrset_data* d = (struct packed_rrset_data*)rrset-> 933933707f3Ssthen entry.data; 934933707f3Ssthen uint8_t labcount; 935933707f3Ssthen int labdiff; 936933707f3Ssthen uint8_t* wn; 937933707f3Ssthen size_t i, wl; 938933707f3Ssthen if(d->rrsig_count == 0) { 939933707f3Ssthen return 1; 940933707f3Ssthen } 941933707f3Ssthen labcount = rrsig_get_labcount(d, d->count + 0); 942933707f3Ssthen /* check rest of signatures identical */ 943933707f3Ssthen for(i=1; i<d->rrsig_count; i++) { 944933707f3Ssthen if(labcount != rrsig_get_labcount(d, d->count + i)) { 945933707f3Ssthen return 0; 946933707f3Ssthen } 947933707f3Ssthen } 948933707f3Ssthen /* OK the rrsigs check out */ 949933707f3Ssthen /* if the RRSIG label count is shorter than the number of actual 950933707f3Ssthen * labels, then this rrset was synthesized from a wildcard. 951933707f3Ssthen * Note that the RRSIG label count doesn't count the root label. */ 952933707f3Ssthen wn = rrset->rk.dname; 953933707f3Ssthen wl = rrset->rk.dname_len; 954933707f3Ssthen /* skip a leading wildcard label in the dname (RFC4035 2.2) */ 955933707f3Ssthen if(dname_is_wild(wn)) { 956933707f3Ssthen wn += 2; 957933707f3Ssthen wl -= 2; 958933707f3Ssthen } 959933707f3Ssthen labdiff = (dname_count_labels(wn) - 1) - (int)labcount; 960933707f3Ssthen if(labdiff > 0) { 961933707f3Ssthen *wc = wn; 962933707f3Ssthen dname_remove_labels(wc, &wl, labdiff); 963938a3a5eSflorian *wc_len = wl; 964933707f3Ssthen return 1; 965933707f3Ssthen } 966933707f3Ssthen return 1; 967933707f3Ssthen } 968933707f3Ssthen 969933707f3Ssthen int 970933707f3Ssthen val_chase_cname(struct query_info* qchase, struct reply_info* rep, 971933707f3Ssthen size_t* cname_skip) { 972933707f3Ssthen size_t i; 973933707f3Ssthen /* skip any DNAMEs, go to the CNAME for next part */ 974933707f3Ssthen for(i = *cname_skip; i < rep->an_numrrsets; i++) { 975933707f3Ssthen if(ntohs(rep->rrsets[i]->rk.type) == LDNS_RR_TYPE_CNAME && 976933707f3Ssthen query_dname_compare(qchase->qname, rep->rrsets[i]-> 977933707f3Ssthen rk.dname) == 0) { 978933707f3Ssthen qchase->qname = NULL; 979933707f3Ssthen get_cname_target(rep->rrsets[i], &qchase->qname, 980933707f3Ssthen &qchase->qname_len); 981933707f3Ssthen if(!qchase->qname) 982933707f3Ssthen return 0; /* bad CNAME rdata */ 983933707f3Ssthen (*cname_skip) = i+1; 984933707f3Ssthen return 1; 985933707f3Ssthen } 986933707f3Ssthen } 987933707f3Ssthen return 0; /* CNAME classified but no matching CNAME ?! */ 988933707f3Ssthen } 989933707f3Ssthen 990933707f3Ssthen /** see if rrset has signer name as one of the rrsig signers */ 991933707f3Ssthen static int 992933707f3Ssthen rrset_has_signer(struct ub_packed_rrset_key* rrset, uint8_t* name, size_t len) 993933707f3Ssthen { 994933707f3Ssthen struct packed_rrset_data* d = (struct packed_rrset_data*)rrset-> 995933707f3Ssthen entry.data; 996933707f3Ssthen size_t i; 997933707f3Ssthen for(i = d->count; i< d->count+d->rrsig_count; i++) { 998933707f3Ssthen if(d->rr_len[i] > 2+18+len) { 999933707f3Ssthen /* at least rdatalen + signature + signame (+1 sig)*/ 1000229e174cSsthen if(!dname_valid(d->rr_data[i]+2+18, d->rr_len[i]-2-18)) 1001229e174cSsthen continue; 1002933707f3Ssthen if(query_dname_compare(name, d->rr_data[i]+2+18) == 0) 1003933707f3Ssthen { 1004933707f3Ssthen return 1; 1005933707f3Ssthen } 1006933707f3Ssthen } 1007933707f3Ssthen } 1008933707f3Ssthen return 0; 1009933707f3Ssthen } 1010933707f3Ssthen 1011933707f3Ssthen void 1012933707f3Ssthen val_fill_reply(struct reply_info* chase, struct reply_info* orig, 1013933707f3Ssthen size_t skip, uint8_t* name, size_t len, uint8_t* signer) 1014933707f3Ssthen { 1015*98bc733bSsthen size_t i, j; 1016933707f3Ssthen int seen_dname = 0; 1017933707f3Ssthen chase->rrset_count = 0; 1018933707f3Ssthen chase->an_numrrsets = 0; 1019933707f3Ssthen chase->ns_numrrsets = 0; 1020933707f3Ssthen chase->ar_numrrsets = 0; 1021933707f3Ssthen /* ANSWER section */ 1022933707f3Ssthen for(i=skip; i<orig->an_numrrsets; i++) { 1023933707f3Ssthen if(!signer) { 1024933707f3Ssthen if(query_dname_compare(name, 1025933707f3Ssthen orig->rrsets[i]->rk.dname) == 0) 1026933707f3Ssthen chase->rrsets[chase->an_numrrsets++] = 1027933707f3Ssthen orig->rrsets[i]; 1028933707f3Ssthen } else if(seen_dname && ntohs(orig->rrsets[i]->rk.type) == 1029933707f3Ssthen LDNS_RR_TYPE_CNAME) { 1030933707f3Ssthen chase->rrsets[chase->an_numrrsets++] = orig->rrsets[i]; 1031933707f3Ssthen seen_dname = 0; 1032933707f3Ssthen } else if(rrset_has_signer(orig->rrsets[i], name, len)) { 1033933707f3Ssthen chase->rrsets[chase->an_numrrsets++] = orig->rrsets[i]; 1034933707f3Ssthen if(ntohs(orig->rrsets[i]->rk.type) == 1035933707f3Ssthen LDNS_RR_TYPE_DNAME) { 1036933707f3Ssthen seen_dname = 1; 1037933707f3Ssthen } 1038*98bc733bSsthen } else if(ntohs(orig->rrsets[i]->rk.type) == LDNS_RR_TYPE_CNAME 1039*98bc733bSsthen && ((struct packed_rrset_data*)orig->rrsets[i]-> 1040*98bc733bSsthen entry.data)->rrsig_count == 0 && 1041*98bc733bSsthen cname_under_previous_dname(orig, i, &j) && 1042*98bc733bSsthen rrset_has_signer(orig->rrsets[j], name, len)) { 1043*98bc733bSsthen chase->rrsets[chase->an_numrrsets++] = orig->rrsets[j]; 1044*98bc733bSsthen chase->rrsets[chase->an_numrrsets++] = orig->rrsets[i]; 1045933707f3Ssthen } 1046933707f3Ssthen } 1047933707f3Ssthen /* AUTHORITY section */ 1048933707f3Ssthen for(i = (skip > orig->an_numrrsets)?skip:orig->an_numrrsets; 1049933707f3Ssthen i<orig->an_numrrsets+orig->ns_numrrsets; 1050933707f3Ssthen i++) { 1051933707f3Ssthen if(!signer) { 1052933707f3Ssthen if(query_dname_compare(name, 1053933707f3Ssthen orig->rrsets[i]->rk.dname) == 0) 1054933707f3Ssthen chase->rrsets[chase->an_numrrsets+ 1055933707f3Ssthen chase->ns_numrrsets++] = orig->rrsets[i]; 1056933707f3Ssthen } else if(rrset_has_signer(orig->rrsets[i], name, len)) { 1057933707f3Ssthen chase->rrsets[chase->an_numrrsets+ 1058933707f3Ssthen chase->ns_numrrsets++] = orig->rrsets[i]; 1059933707f3Ssthen } 1060933707f3Ssthen } 1061933707f3Ssthen /* ADDITIONAL section */ 1062933707f3Ssthen for(i= (skip>orig->an_numrrsets+orig->ns_numrrsets)? 1063933707f3Ssthen skip:orig->an_numrrsets+orig->ns_numrrsets; 1064933707f3Ssthen i<orig->rrset_count; i++) { 1065933707f3Ssthen if(!signer) { 1066933707f3Ssthen if(query_dname_compare(name, 1067933707f3Ssthen orig->rrsets[i]->rk.dname) == 0) 1068933707f3Ssthen chase->rrsets[chase->an_numrrsets 1069933707f3Ssthen +orig->ns_numrrsets+chase->ar_numrrsets++] 1070933707f3Ssthen = orig->rrsets[i]; 1071933707f3Ssthen } else if(rrset_has_signer(orig->rrsets[i], name, len)) { 1072933707f3Ssthen chase->rrsets[chase->an_numrrsets+orig->ns_numrrsets+ 1073933707f3Ssthen chase->ar_numrrsets++] = orig->rrsets[i]; 1074933707f3Ssthen } 1075933707f3Ssthen } 1076933707f3Ssthen chase->rrset_count = chase->an_numrrsets + chase->ns_numrrsets + 1077933707f3Ssthen chase->ar_numrrsets; 1078933707f3Ssthen } 1079933707f3Ssthen 10803b25f654Sbrad void val_reply_remove_auth(struct reply_info* rep, size_t index) 10813b25f654Sbrad { 10823b25f654Sbrad log_assert(index < rep->rrset_count); 10833b25f654Sbrad log_assert(index >= rep->an_numrrsets); 10843b25f654Sbrad log_assert(index < rep->an_numrrsets+rep->ns_numrrsets); 10853b25f654Sbrad memmove(rep->rrsets+index, rep->rrsets+index+1, 10863b25f654Sbrad sizeof(struct ub_packed_rrset_key*)* 10873b25f654Sbrad (rep->rrset_count - index - 1)); 10883b25f654Sbrad rep->ns_numrrsets--; 10893b25f654Sbrad rep->rrset_count--; 10903b25f654Sbrad } 10913b25f654Sbrad 1092933707f3Ssthen void 10932be9e038Ssthen val_check_nonsecure(struct module_env* env, struct reply_info* rep) 1094933707f3Ssthen { 1095933707f3Ssthen size_t i; 1096933707f3Ssthen /* authority */ 1097933707f3Ssthen for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) { 1098933707f3Ssthen if(((struct packed_rrset_data*)rep->rrsets[i]->entry.data) 1099933707f3Ssthen ->security != sec_status_secure) { 1100933707f3Ssthen /* because we want to return the authentic original 1101933707f3Ssthen * message when presented with CD-flagged queries, 1102933707f3Ssthen * we need to preserve AUTHORITY section data. 1103933707f3Ssthen * However, this rrset is not signed or signed 1104933707f3Ssthen * with the wrong keys. Validation has tried to 1105933707f3Ssthen * verify this rrset with the keysets of import. 1106933707f3Ssthen * But this rrset did not verify. 1107933707f3Ssthen * Therefore the message is bogus. 1108933707f3Ssthen */ 1109933707f3Ssthen 11107191de28Ssthen /* check if authority has an NS record 1111933707f3Ssthen * which is bad, and there is an answer section with 1112933707f3Ssthen * data. In that case, delete NS and additional to 1113933707f3Ssthen * be lenient and make a minimal response */ 11147191de28Ssthen if(rep->an_numrrsets != 0 && 1115933707f3Ssthen ntohs(rep->rrsets[i]->rk.type) 1116933707f3Ssthen == LDNS_RR_TYPE_NS) { 1117933707f3Ssthen verbose(VERB_ALGO, "truncate to minimal"); 1118933707f3Ssthen rep->ar_numrrsets = 0; 11197191de28Ssthen rep->rrset_count = rep->an_numrrsets + 11207191de28Ssthen rep->ns_numrrsets; 11217191de28Ssthen /* remove this unneeded authority rrset */ 11227191de28Ssthen memmove(rep->rrsets+i, rep->rrsets+i+1, 11237191de28Ssthen sizeof(struct ub_packed_rrset_key*)* 11247191de28Ssthen (rep->rrset_count - i - 1)); 11257191de28Ssthen rep->ns_numrrsets--; 11267191de28Ssthen rep->rrset_count--; 11277191de28Ssthen i--; 1128933707f3Ssthen return; 1129933707f3Ssthen } 1130933707f3Ssthen 1131933707f3Ssthen log_nametypeclass(VERB_QUERY, "message is bogus, " 1132933707f3Ssthen "non secure rrset", 1133933707f3Ssthen rep->rrsets[i]->rk.dname, 1134933707f3Ssthen ntohs(rep->rrsets[i]->rk.type), 1135933707f3Ssthen ntohs(rep->rrsets[i]->rk.rrset_class)); 1136933707f3Ssthen rep->security = sec_status_bogus; 1137933707f3Ssthen return; 1138933707f3Ssthen } 1139933707f3Ssthen } 1140933707f3Ssthen /* additional */ 11412be9e038Ssthen if(!env->cfg->val_clean_additional) 1142933707f3Ssthen return; 1143933707f3Ssthen for(i=rep->an_numrrsets+rep->ns_numrrsets; i<rep->rrset_count; i++) { 1144933707f3Ssthen if(((struct packed_rrset_data*)rep->rrsets[i]->entry.data) 1145933707f3Ssthen ->security != sec_status_secure) { 1146933707f3Ssthen /* This does not cause message invalidation. It was 1147933707f3Ssthen * simply unsigned data in the additional. The 1148933707f3Ssthen * RRSIG must have been truncated off the message. 1149933707f3Ssthen * 1150933707f3Ssthen * However, we do not want to return possible bogus 1151933707f3Ssthen * data to clients that rely on this service for 1152933707f3Ssthen * their authentication. 1153933707f3Ssthen */ 1154933707f3Ssthen /* remove this unneeded additional rrset */ 1155933707f3Ssthen memmove(rep->rrsets+i, rep->rrsets+i+1, 1156933707f3Ssthen sizeof(struct ub_packed_rrset_key*)* 1157933707f3Ssthen (rep->rrset_count - i - 1)); 1158933707f3Ssthen rep->ar_numrrsets--; 1159933707f3Ssthen rep->rrset_count--; 1160933707f3Ssthen i--; 1161933707f3Ssthen } 1162933707f3Ssthen } 1163933707f3Ssthen } 1164933707f3Ssthen 1165933707f3Ssthen /** check no anchor and unlock */ 1166933707f3Ssthen static int 1167933707f3Ssthen check_no_anchor(struct val_anchors* anchors, uint8_t* nm, size_t l, uint16_t c) 1168933707f3Ssthen { 1169933707f3Ssthen struct trust_anchor* ta; 1170933707f3Ssthen if((ta=anchors_lookup(anchors, nm, l, c))) { 1171933707f3Ssthen lock_basic_unlock(&ta->lock); 1172933707f3Ssthen } 1173933707f3Ssthen return !ta; 1174933707f3Ssthen } 1175933707f3Ssthen 1176933707f3Ssthen void 1177933707f3Ssthen val_mark_indeterminate(struct reply_info* rep, struct val_anchors* anchors, 1178933707f3Ssthen struct rrset_cache* r, struct module_env* env) 1179933707f3Ssthen { 1180933707f3Ssthen size_t i; 1181933707f3Ssthen struct packed_rrset_data* d; 1182933707f3Ssthen for(i=0; i<rep->rrset_count; i++) { 1183933707f3Ssthen d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data; 1184933707f3Ssthen if(d->security == sec_status_unchecked && 1185933707f3Ssthen check_no_anchor(anchors, rep->rrsets[i]->rk.dname, 1186933707f3Ssthen rep->rrsets[i]->rk.dname_len, 1187933707f3Ssthen ntohs(rep->rrsets[i]->rk.rrset_class))) 1188933707f3Ssthen { 1189933707f3Ssthen /* mark as indeterminate */ 1190933707f3Ssthen d->security = sec_status_indeterminate; 1191933707f3Ssthen rrset_update_sec_status(r, rep->rrsets[i], *env->now); 1192933707f3Ssthen } 1193933707f3Ssthen } 1194933707f3Ssthen } 1195933707f3Ssthen 1196933707f3Ssthen void 1197933707f3Ssthen val_mark_insecure(struct reply_info* rep, uint8_t* kname, 1198933707f3Ssthen struct rrset_cache* r, struct module_env* env) 1199933707f3Ssthen { 1200933707f3Ssthen size_t i; 1201933707f3Ssthen struct packed_rrset_data* d; 1202933707f3Ssthen for(i=0; i<rep->rrset_count; i++) { 1203933707f3Ssthen d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data; 1204933707f3Ssthen if(d->security == sec_status_unchecked && 1205933707f3Ssthen dname_subdomain_c(rep->rrsets[i]->rk.dname, kname)) { 1206933707f3Ssthen /* mark as insecure */ 1207933707f3Ssthen d->security = sec_status_insecure; 1208933707f3Ssthen rrset_update_sec_status(r, rep->rrsets[i], *env->now); 1209933707f3Ssthen } 1210933707f3Ssthen } 1211933707f3Ssthen } 1212933707f3Ssthen 1213933707f3Ssthen size_t 1214933707f3Ssthen val_next_unchecked(struct reply_info* rep, size_t skip) 1215933707f3Ssthen { 1216933707f3Ssthen size_t i; 1217933707f3Ssthen struct packed_rrset_data* d; 1218933707f3Ssthen for(i=skip+1; i<rep->rrset_count; i++) { 1219933707f3Ssthen d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data; 1220933707f3Ssthen if(d->security == sec_status_unchecked) { 1221933707f3Ssthen return i; 1222933707f3Ssthen } 1223933707f3Ssthen } 1224933707f3Ssthen return rep->rrset_count; 1225933707f3Ssthen } 1226933707f3Ssthen 1227933707f3Ssthen const char* 1228933707f3Ssthen val_classification_to_string(enum val_classification subtype) 1229933707f3Ssthen { 1230933707f3Ssthen switch(subtype) { 1231933707f3Ssthen case VAL_CLASS_UNTYPED: return "untyped"; 1232933707f3Ssthen case VAL_CLASS_UNKNOWN: return "unknown"; 1233933707f3Ssthen case VAL_CLASS_POSITIVE: return "positive"; 1234933707f3Ssthen case VAL_CLASS_CNAME: return "cname"; 1235933707f3Ssthen case VAL_CLASS_NODATA: return "nodata"; 1236933707f3Ssthen case VAL_CLASS_NAMEERROR: return "nameerror"; 1237933707f3Ssthen case VAL_CLASS_CNAMENOANSWER: return "cnamenoanswer"; 1238933707f3Ssthen case VAL_CLASS_REFERRAL: return "referral"; 1239933707f3Ssthen case VAL_CLASS_ANY: return "qtype_any"; 1240933707f3Ssthen default: 1241933707f3Ssthen return "bad_val_classification"; 1242933707f3Ssthen } 1243933707f3Ssthen } 1244933707f3Ssthen 1245933707f3Ssthen /** log a sock_list entry */ 1246933707f3Ssthen static void 1247933707f3Ssthen sock_list_logentry(enum verbosity_value v, const char* s, struct sock_list* p) 1248933707f3Ssthen { 1249933707f3Ssthen if(p->len) 1250933707f3Ssthen log_addr(v, s, &p->addr, p->len); 1251933707f3Ssthen else verbose(v, "%s cache", s); 1252933707f3Ssthen } 1253933707f3Ssthen 1254933707f3Ssthen void val_blacklist(struct sock_list** blacklist, struct regional* region, 1255933707f3Ssthen struct sock_list* origin, int cross) 1256933707f3Ssthen { 1257933707f3Ssthen /* debug printout */ 1258933707f3Ssthen if(verbosity >= VERB_ALGO) { 1259933707f3Ssthen struct sock_list* p; 1260933707f3Ssthen for(p=*blacklist; p; p=p->next) 1261933707f3Ssthen sock_list_logentry(VERB_ALGO, "blacklist", p); 1262933707f3Ssthen if(!origin) 1263933707f3Ssthen verbose(VERB_ALGO, "blacklist add: cache"); 1264933707f3Ssthen for(p=origin; p; p=p->next) 1265933707f3Ssthen sock_list_logentry(VERB_ALGO, "blacklist add", p); 1266933707f3Ssthen } 1267933707f3Ssthen /* blacklist the IPs or the cache */ 1268933707f3Ssthen if(!origin) { 1269933707f3Ssthen /* only add if nothing there. anything else also stops cache*/ 1270933707f3Ssthen if(!*blacklist) 1271933707f3Ssthen sock_list_insert(blacklist, NULL, 0, region); 1272933707f3Ssthen } else if(!cross) 1273933707f3Ssthen sock_list_prepend(blacklist, origin); 1274933707f3Ssthen else sock_list_merge(blacklist, region, origin); 1275933707f3Ssthen } 1276933707f3Ssthen 1277933707f3Ssthen int val_has_signed_nsecs(struct reply_info* rep, char** reason) 1278933707f3Ssthen { 1279933707f3Ssthen size_t i, num_nsec = 0, num_nsec3 = 0; 1280933707f3Ssthen struct packed_rrset_data* d; 1281933707f3Ssthen for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) { 1282933707f3Ssthen if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NSEC)) 1283933707f3Ssthen num_nsec++; 1284933707f3Ssthen else if(rep->rrsets[i]->rk.type == htons(LDNS_RR_TYPE_NSEC3)) 1285933707f3Ssthen num_nsec3++; 1286933707f3Ssthen else continue; 1287933707f3Ssthen d = (struct packed_rrset_data*)rep->rrsets[i]->entry.data; 1288933707f3Ssthen if(d && d->rrsig_count != 0) { 1289933707f3Ssthen return 1; 1290933707f3Ssthen } 1291933707f3Ssthen } 1292933707f3Ssthen if(num_nsec == 0 && num_nsec3 == 0) 1293933707f3Ssthen *reason = "no DNSSEC records"; 1294933707f3Ssthen else if(num_nsec != 0) 1295933707f3Ssthen *reason = "no signatures over NSECs"; 1296933707f3Ssthen else *reason = "no signatures over NSEC3s"; 1297933707f3Ssthen return 0; 1298933707f3Ssthen } 1299933707f3Ssthen 1300933707f3Ssthen struct dns_msg* 1301933707f3Ssthen val_find_DS(struct module_env* env, uint8_t* nm, size_t nmlen, uint16_t c, 1302933707f3Ssthen struct regional* region, uint8_t* topname) 1303933707f3Ssthen { 1304933707f3Ssthen struct dns_msg* msg; 1305933707f3Ssthen struct query_info qinfo; 1306933707f3Ssthen struct ub_packed_rrset_key *rrset = rrset_cache_lookup( 1307933707f3Ssthen env->rrset_cache, nm, nmlen, LDNS_RR_TYPE_DS, c, 0, 1308933707f3Ssthen *env->now, 0); 1309933707f3Ssthen if(rrset) { 1310933707f3Ssthen /* DS rrset exists. Return it to the validator immediately*/ 1311933707f3Ssthen struct ub_packed_rrset_key* copy = packed_rrset_copy_region( 1312933707f3Ssthen rrset, region, *env->now); 1313933707f3Ssthen lock_rw_unlock(&rrset->entry.lock); 1314933707f3Ssthen if(!copy) 1315933707f3Ssthen return NULL; 1316933707f3Ssthen msg = dns_msg_create(nm, nmlen, LDNS_RR_TYPE_DS, c, region, 1); 1317933707f3Ssthen if(!msg) 1318933707f3Ssthen return NULL; 1319933707f3Ssthen msg->rep->rrsets[0] = copy; 1320933707f3Ssthen msg->rep->rrset_count++; 1321933707f3Ssthen msg->rep->an_numrrsets++; 1322933707f3Ssthen return msg; 1323933707f3Ssthen } 1324933707f3Ssthen /* lookup in rrset and negative cache for NSEC/NSEC3 */ 1325933707f3Ssthen qinfo.qname = nm; 1326933707f3Ssthen qinfo.qname_len = nmlen; 1327933707f3Ssthen qinfo.qtype = LDNS_RR_TYPE_DS; 1328933707f3Ssthen qinfo.qclass = c; 132977079be7Ssthen qinfo.local_alias = NULL; 1330933707f3Ssthen /* do not add SOA to reply message, it is going to be used internal */ 1331933707f3Ssthen msg = val_neg_getmsg(env->neg_cache, &qinfo, region, env->rrset_cache, 1332938a3a5eSflorian env->scratch_buffer, *env->now, 0, topname, env->cfg); 1333933707f3Ssthen return msg; 1334933707f3Ssthen } 1335